Nginx
Nginx架构图
Nginx for Docker for Linux
一.下载 Nginx 镜像
docker pull nginx:1.27
二.运行 Nginx 容器
docker run -d -p 80:80 --name nginx nginx:1.27.3
#-p 80:80: 将宿主机的 80 端口映射到容器的 80 端口上,
#-d: 以后台方式运行镜像;
#--name: 指定容器的名称为 nginx;
1.创建 Nginx 挂载目录
mkdir -p /docker/nginx
mkdir -p /docker/nginx/html
mkdir -p /docker/nginx/cert
mkdir -p /docker/nginx/logs
2.确保该目录的权限允许 Docker 容器访问。可以尝试更改目录的权限,确保它对 Docker 容器可写:
sudo chown -R 1001:1001 /docker/nginx
sudo chmod -R 755 /docker/nginx
sudo chown -R 1001:1001 /docker/nginx/html
sudo chmod -R 755 /docker/nginx/html
sudo chown -R 1001:1001 /docker/nginx/cert
sudo chmod -R 755 /docker/nginx/cert
sudo chown -R 1001:1001 /docker/nginx/logs
sudo chmod -R 755 /docker/nginx/logs
3.1查看nginx的启动用户
ps aux|grep nginx
ps aux | grep "nginx: worker process" | awk '{print $1}'
3.2使用root用户运行 Docker,需要重新启动 Docker 服务并确保它以 root 权限启动:
sudo systemctl restart docker
4.复制 Nginx 配置文件至宿主机
# 复制名称为 nginx 容器中 /etc/nginx/nginx.conf 文件夹到宿主机的 /docker/nginx 路径下
docker cp nginx:/etc/nginx/nginx.conf /docker/nginx
# 复制名称为 nginx 容器中 /etc/nginx/conf.d 文件到宿主机的 /docker/nginx 路径下
docker cp nginx:/etc/nginx/conf.d /docker/nginx
三.修改 Nginx 配置文件
1.编辑/docker/nginx/conf.d/default.conf
cd /docker/nginx/conf.d
vim default.conf
完整/docker/nginx/default.conf内容如下
server {
listen 80;
listen [::]:80;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#location ~ /\.ht {
# deny all;
#}
}
2.新增网站
新建www_yangmufa_ssl.conf文件
sudo touch /docker/nginx/conf.d/www_devcaikun_ssl.conf
编辑www_devcaikun_ssl.conf文件
vim /docker/nginx/conf.d/www_devcaikun_ssl.conf
www_devcaikun_ssl.conf内容如下
# 监听80端口(HTTP)
server {
listen 80;
listen [::]:80;
server_name www.yangmufa.cn;
client_max_body_size 50M; # 设置客户端请求最大体积为50M
# 配置根路径,处理根目录下的请求
location / {
try_files $uri $uri/ @router; # 尝试按原路径访问文件或目录,如果失败则转发到@router
root /usr/share/nginx/html/devcaikun/; # 网站根目录
index index.html index.htm; # 默认首页文件
}
# 当以上路径都无法匹配时,所有请求都重定向到 /index.html
location @router {
rewrite ^.*$ /index.html last; # 将所有未匹配的请求重写到 /index.html
}
location /api {
rewrite ^/api/(.*)$ /\$1 break;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://172.17.0.1:8181;
# 设置超时时间
proxy_connect_timeout 600s;
proxy_read_timeout 600s;
proxy_send_timeout 600s;
}
# 错误页面配置,处理50x错误
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html/devcaikun; # 错误页面的存放目录
}
}
3.把静态文件上传到/docker/nginx/html/devcaikun
4.重新运行容器,并挂载对应新增的文件夹
docker stop nginx
docker rm -f nginx
5.重新运行容器
docker run -d -p 80:80 --name nginx -v /docker/nginx/nginx.conf:/etc/nginx/nginx.conf -v /docker/nginx/conf.d:/etc/nginx/conf.d -v /docker/nginx/logs:/var/log/nginx -v /docker/nginx/html:/usr/share/nginx/html nginx:1.27.3
四.HTTPS-SSL证书(可选)
1.把 fullchain.yangmufa.cn.pem 和 private.yangmufa.cn.key 这两个证书文件上传到 /docker/nginx/cert/ 文件夹下
2.修改/docker/nginx/conf.d/www_devcaikun_ssl.conf
vim /docker/nginx/conf.d/www_devcaikun_ssl.conf
添加如下内容
# 将所有HTTP请求重定向到HTTPS
return 301 https://www.yangmufa.cn$request_uri;
# SSL配置
ssl_certificate /etc/nginx/cert/fullchain.yangmufa.cn.pem;
ssl_certificate_key /etc/nginx/cert/private.yangmufa.cn.key;
ssl_session_timeout 500m; # SSL会话超时时间
ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!aNULL:!MD5:!ADH:!RC4:!NULL'; # SSL密码套件
ssl_protocols TLSv1.2 TLSv1.3; # 支持的SSL协议
ssl_prefer_server_ciphers on; # 使用服务器优先的密码套件顺序
完整www_devcaikun_ssl.conf内容如下
# 监听80端口(HTTP)
server {
listen 80;
listen [::]:80;
server_name www.yangmufa.cn;
# 将所有HTTP请求重定向到HTTPS
return 301 https://www.yangmufa.cn$request_uri;
}
# 监听443端口(HTTPS)
server {
listen 443 ssl;
listen [::]:443 ssl;
http2 on; # 开启HTTP/2支持
server_name www.yangmufa.cn;
client_max_body_size 50M; # 设置客户端请求最大体积为50M
# SSL配置
ssl_certificate /etc/nginx/cert/fullchain.yangmufa.cn.pem;
ssl_certificate_key /etc/nginx/cert/private.yangmufa.cn.key;
ssl_session_timeout 500m; # SSL会话超时时间
ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!aNULL:!MD5:!ADH:!RC4:!NULL'; # SSL密码套件
ssl_protocols TLSv1.2 TLSv1.3; # 支持的SSL协议
ssl_prefer_server_ciphers on; # 使用服务器优先的密码套件顺序
# 配置根路径,处理根目录下的请求
location / {
#try_files $uri $uri/ /index.html; # 尝试访问请求的文件,若不存在则访问/index.html
try_files $uri $uri/ @router; # 尝试按原路径访问文件或目录,如果失败则转发到@router
root /usr/share/nginx/html/devcaikun/; # 网站根目录
index index.html index.htm; # 默认首页文件
}
# 当以上路径都无法匹配时,所有请求都重定向到 /index.html
location @router {
rewrite ^.*$ /index.html last; # 将所有未匹配的请求重写到 /index.html
}
location /api {
rewrite ^/api/(.*)$ /$1 break;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#allow 0.0.0.0;
#deny all;
proxy_pass http://172.17.0.1:8080;
# Set timeout values
proxy_connect_timeout 600s;
proxy_read_timeout 600s;
proxy_send_timeout 600s;
}
# 错误页面配置,处理50x错误
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html/devcaikun; # 错误页面的存放目录
}
}
3.重新运行容器,并挂载前端文件,需要先将正在运行的 Nginx 容器删除掉
docker stop nginx
docker rm -f nginx
4.重新运行容器
#将docker 的挂载卷 设置为 -v /root/ssl/letsencrypt:/etc/nginx/cert 然后nginx 的default.conf 中的证书位置为 /etc/nginx/cert/live/xxxx/fullchain.pem 即可,这样即使取 ../../archive/xxxx/fullchain.pem 也可以取到
docker run -d -p 80:80 -p 443:443 --name nginx -v /docker/nginx/nginx.conf:/etc/nginx/nginx.conf -v /docker/nginx/conf.d:/etc/nginx/conf.d -v /docker/nginx/logs:/var/log/nginx -v /docker/nginx/cert:/etc/nginx/cert -v /docker/nginx/html:/usr/share/nginx/html nginx:1.27.3
#-p 80:80 -p 443:443:将容器的 80、443 端口映射到主机的 80、443 端口;
--restart=always: Docker.md 重启时,容器也跟着重启;
#-v /docker/nginx/nginx.conf:/etc/nginx/nginx.conf:将容器中的 /etc/nginx/nginx.conf 文件挂载到宿主机中的 /docker/nginx/nginx.conf 文件;
#-v /docker/nginx/conf.d:/etc/nginx/conf.d:将容器中 /etc/nginx/conf.d 目录挂载到宿主机中的 conf.d 目录下;
#-v /docker/nginx/logs:/var/log/nginx:将容器中的 /var/log/nginx 目录挂载到宿主机中的 /docker/nginx/logs 目录下, 用以查看 Nginx 日志;
#-v /docker/nginx/cert:/etc/nginx/cert : 将容器中的 /etc/nginx/cert 目录挂载到宿主机中 /docker/nginx/cert:/etc/nginx/cert 目录下,用于容器内部能够正常读取到 SSL 证书;
#-v /docker/nginx/html:/usr/share/nginx/html : 将前端工程所在的目录,挂载到容器中的 /usr/share/nginx/html 文件夹 上;
# 还需要将云服务器的 443 端口加入到安全组中,才能保证外界的正常访问 https 端口.
5.还需要将云服务器的 443 端口加入到安全组中,才能保证外界的正常访问 https 端口.
6.测试: 在命令行发起请求类似axios请求
www.yangmufa.cn
五.开启 Gzip 压缩(可选)
cd /docker/nginx/
vim nginx.conf
添加内容如下
# 开启 gzip
gzip on;
# 启用 gzip 压缩的最小文件,小于设置值的文件则不会被压缩
gzip_min_length 1k;
# 设置压缩所需要的缓冲区大小
gzip_buffers 32 4k;
# 设置 gzip 压缩针对的 HTTP 协议版本
gzip_http_version 1.1;
# gzip 压缩级别,1-9,数字越大压缩的越好,也越占用CPU时间
gzip_comp_level 6;
# 进行压缩的文件类型,javascript 有多种形式。
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php application/javascript application/json;
# IE 6 以下浏览器不启用 gzip (因为 IE 6 以下不支持)
gzip_disable "MSIE [1-6]\.";
# 是否在 http header 中添加 Vary: Accept-Encoding,建议开启
gzip_vary on;
/docker/nginx/nginx.conf完整内容如下
user root;
#user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
# 开启 gzip
gzip on;
# 启用 gzip 压缩的最小文件,小于设置值的文件则不会被压缩
gzip_min_length 1k;
# 设置压缩所需要的缓冲区大小
gzip_buffers 32 4k;
# 设置 gzip 压缩针对的 HTTP 协议版本
gzip_http_version 1.1;
# gzip 压缩级别,1-9,数字越大压缩的越好,也越占用CPU时间
gzip_comp_level 6;
# 进行压缩的文件类型,javascript 有多种形式。
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php application/javascript application/json;
# IE 6 以下浏览器不启用 gzip (因为 IE 6 以下不支持)
gzip_disable "MSIE [1-6]\.";
# 是否在 http header 中添加 Vary: Accept-Encoding,建议开启
gzip_vary on;
}
重启 Nginx 容器,让容器加载刚刚新添加的配置文件
docker restart nginx
Nginx for Docker Desktop for Windows
1.安装 Docker Desktop
详情见 Docker
2.拉取 Nginx 镜像
-
打开 PowerShell 或命令提示符(CMD)。
-
输入以下命令下载官方 Nginx 镜像:
docker pull nginx:1.27.4
3.运行 Nginx 容器
基础步骤
# Windows对应Docker挂载目录
# 主配置文件:-v /path/to/conf.d:/etc/nginx/conf.d
# 默认站点配置文件:-v /path/to/default:/etc/nginx/sites-enabled/default
# 站点配置文件目录:-v /path/to/conf.d:/etc/nginx/conf.d
# SSL证书文件目录:-v /path/to/ssl:/etc/nginx/ssl
# 默认静态文件目录:-v /path/to/html:/usr/share/nginx/html
# 静态资源目录:-v /path/to/static:/usr/share/nginx/static
# 缓存目录:-v /path/to/cache:/var/cache/nginx
# 挂载日志目录:-v /path/to/logs:/var/log/nginx
# 环境变量文件:-v /path/to/envfile:/etc/nginx/envfile
# 1.创建容器挂载需要的文件夹
mkdir E:\DevelopmentKitProject\Docker\Nginx # 默认站点配置文件目录
mkdir E:\DevelopmentKitProject\Docker\Nginx\html # 主配置文件
mkdir E:\DevelopmentKitProject\Docker\Nginx\conf.d # 站点配置文件目录
mkdir E:\DevelopmentKitProject\Docker\Nginx\ssl # SSL证书文件目录
mkdir E:\DevelopmentKitProject\Docker\Nginx\cache # 缓存目录
mkdir E:\DevelopmentKitProject\Docker\Nginx\log # 日志目录
# 最终目录结构
E:\DevelopmentKitProject\Docker\Nginx\
├── html\
│ ├── project1\
│ │ ├── index.html
│ │ └── static\
│ └── project2\
│ ├── index.html
│ └── static\
├── conf.d\
│ ├── project1.conf
│ └── project2.conf
├── nginx.conf
├── cache\
├── log\
└── ssl\
# 2.先运行一个临时容器
docker run --name temp-nginx -d nginx:1.27.4
# 3.复制nginx.conf到Windows自定义文件夹
docker cp temp-nginx:/etc/nginx/nginx.conf E:\\DevelopmentKitProject\\Docker\\Nginx\\nginx.conf
# 4.删除临时容器
docker rm -f temp-nginx
# 5.运行一个容器
docker run --name Nginx127 -p 8080:80
-v E:\\DevelopmentKitProject\\Docker\\Nginx\\html:/usr/share/nginx/html
-v E:\\DevelopmentKitProject\\Docker\\Nginx\\nginx.conf:/etc/nginx/nginx.conf
-v E:\\DevelopmentKitProject\\Docker\\Nginx\\cache:/var/cache/nginx
-v E:\\DevelopmentKitProject\\Docker\\Nginx\\log:/var/log/nginx
-v E:\\DevelopmentKitProject\\Docker\\Nginx\\ssl:/etc/nginx/ssl
-v E:\\DevelopmentKitProject\\Docker\\Nginx\\conf.d:/etc/nginx/conf.d
-d nginx:1.27.4
# --name Nginx127 容器命名为 Nginx127
# -p 80:80 将宿主机的 80 端口映射到容器的 80 端口
# -d 后台运行
# nginx:1.27.4 使用的镜像名称和版本
# -v E:\\DevelopmentKitProject\\Docker\\Nginx\\html:/usr/share/nginx/html 挂载本地 HTML 文件到容器
# -v E:\\DevelopmentKitProject\\Docker\\Nginx\\nginx.conf:/etc/nginx/nginx.conf 挂载自定义配置文件
3.1常见问题
-
端口冲突
- 如果 80 端口被占用,修改映射端口(例如
-p 8080:80
),然后访问http://localhost:8080
。
- 如果 80 端口被占用,修改映射端口(例如
-
权限问题
- Windows 路径需使用绝对路径(如
D:\nginx
),路径中的反斜杠可能需要转义为/
或使用 PowerShell 的反引号 ```。
- Windows 路径需使用绝对路径(如
-
配置文件错误
-
挂载自定义
nginx.conf
前,建议先复制默认配置:docker cp my-nginx:/etc/nginx/nginx.conf E:\DevelopmentKitProject\Docker\Nginx\nginx.conf
-
-
镜像下载缓慢
- 配置 Docker 国内镜像源(阿里云、腾讯云等),在 Docker Desktop 设置中修改
registry-mirrors
。
- 配置 Docker 国内镜像源(阿里云、腾讯云等),在 Docker Desktop 设置中修改
4.配置 Nginx
在 conf.d
目录中为每个项目创建一个独立的配置.conf文件。
4.1项目 1 的配置文件:project1.conf
在 E:\DevelopmentKitProject\Docker\Nginx\conf.d 文件夹下创建project1.conf 文件并编辑内容如下
# 监听80端口(HTTP)
server {
listen 80;
server_name project1.local; # 域名或主机名
client_max_body_size 500M; # 设置客户端请求最大体积为500M
# 日志文件
access_log /var/log/nginx/project1_access.log;
error_log /var/log/nginx/project1_error.log;
# 配置根路径,处理根目录下的请求
location / {
try_files $uri $uri/ @router; # 尝试按原路径访问文件或目录,如果失败则转发到@router
root /usr/share/nginx/html/project1; # 网站根目录
index index.html index.htm; # 默认首页文件
}
#当以上路径都无法匹配时,所有请求都重定向到 /index.html
location @router {
#rewrite ^.*$ /404.html last; # 将所有未匹配的请求重写到 /index.html
# 如果你的项目是一个单页应用(SPA),前端路由(如React Router、Vue Router等)可能会处理URL路径。
# 当用户刷新页面时,Nginx会尝试根据URL路径查找对应的文件,但由于SPA的路由是虚拟的,
# Nginx找不到对应的文件,因此会触发location @router,导致显示此404页面
rewrite ^.*$ /index.html last; # 将所有未匹配的请求重写到 /index.html
#将所有未匹配的请求重定向到/index.html(这是SPA的常见做法)
}
# 错误页面配置,处理50x错误
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/error_page; # 错误页面的存放目录
}
# 其他配置(如 API 代理)
location /api/ {
proxy_pass http://127.0.0.1:8181; # 后端 API 地址
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
4.2项目 2 的配置文件:project2.conf
在 E:\DevelopmentKitProject\Docker\Nginx\conf.d 文件夹下创建project2.conf 文件并编辑内容如下
# 监听80端口(HTTP)
server {
listen 80;
server_name project2.local; # 域名或主机名
client_max_body_size 500M; # 设置客户端请求最大体积为500M
# 日志文件
access_log /var/log/nginx/project2_access.log;
error_log /var/log/nginx/project2_error.log;
# 配置根路径,处理根目录下的请求
location / {
try_files $uri $uri/ @router; # 尝试按原路径访问文件或目录,如果失败则转发到@router
root /usr/share/nginx/html/project2; # 网站根目录
index index.html index.htm; # 默认首页文件
}
#当以上路径都无法匹配时,所有请求都重定向到 /index.html
location @router {
#rewrite ^.*$ /404.html last; # 将所有未匹配的请求重写到 /index.html
# 如果你的项目是一个单页应用(SPA),前端路由(如React Router、Vue Router等)可能会处理URL路径。
# 当用户刷新页面时,Nginx会尝试根据URL路径查找对应的文件,但由于SPA的路由是虚拟的,
# Nginx找不到对应的文件,因此会触发location @router,导致显示此404页面
rewrite ^.*$ /index.html last; # 将所有未匹配的请求重写到 /index.html
#将所有未匹配的请求重定向到/index.html(这是SPA的常见做法)
}
# 错误页面配置,处理50x错误
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/error_page; # 错误页面的存放目录
}
# 其他配置(如 API 代理)
location /api/ {
proxy_pass http://127.0.0.1:8181; # 后端 API 地址
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
5. 修改 nginx.conf
确保 E:\DevelopmentKitProject\Docker\Nginx\nginx.conf
文件的内容中包含了 conf.d 目录的配置加载:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
# 加载 conf.d 目录中的所有配置文件
include /etc/nginx/conf.d/*.conf;
}
6. 修改Windows电脑 hosts 文件
为了通过本地自定义域名访问项目,需要在本地 hosts
文件中添加域名解析:
-
打开
C:\Windows\System32\drivers\etc\hosts
文件。 -
添加以下内容:
127.0.0.1 project1.local
127.0.0.1 project2.local -
完整示例
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 103.55.97.87 rhino.acme.com # source server
# 39.24.62.11 x.acme.com # x client host
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
# Added by Docker Desktop
192.168.0.106 host.docker.internal
192.168.0.109 gateway.docker.internal
# To allow the same kube context to work on the host and the container:
127.0.0.1 kubernetes.docker.internal
# End of section
# 添加内容如下
127.0.0.1 project1.local
127.0.0.1 project2.local
7. 重启Windows和容器
8.检查
8.1. 检查本地 hosts 文件
确保 project1.local
已正确映射到 127.0.0.1
。
1.在命令提示符(CMD)或 PowerShell 中运行以下命令,检查域名解析是否正确:
ping project1.local
2.如果解析正确,应该看到类似以下的输出:
Pinging project1.local [127.0.0.1] with 32 bytes of data:
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
8.2. 检查 Nginx 配置
确保 project1.conf
配置正确,并且 Nginx 已加载该配置。
1.进入容器内部,检查配置文件是否正确加载:
docker exec -it Nginx127 /bin/sh
nginx -t
2.如果配置正确会有如下显示:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
8.3. 检查容器日志
查看容器日志,检查是否有错误信息。
1.运行以下命令查看日志:
docker logs Nginx127
2.如果有错误信息(如文件路径错误或权限问题),根据日志内容进行修复。
8.4. 检查端口映射
确保容器端口正确映射到宿主机的 8080
端口。
-
运行以下命令查看容器端口映射:
docker port Nginx127
-
确保输出类似以下内容:
80/tcp -> 0.0.0.0:8080
8.5. 测试直接访问 IP
尝试通过 IP 地址访问项目,确认是否是域名解析问题。
访问 http://127.0.0.1:8080
,如果能够正常访问,说明问题出在域名解析或 Nginx 的 server_name
配置。
9. 重启Windows和容器
- 访问
http://project1.local:8080
查看项目 1。 - 访问
http://project2.local:8080
查看项目 2。
10. 其他注意事项
- HTTPS 支持:如果需要 HTTPS,可以在
conf.d
中添加 SSL 配置,并挂载 SSL 证书。 - 负载均衡:如果有多个后端服务,可以使用
upstream
配置负载均衡。 - 缓存优化:根据项目需求,配置静态文件缓存。
- 开VPN时:http://project1.local:8080 这种形式将无法访问。