本文最后更新于 2025年8月24日 下午
部署
准备
需要先提前安装docker和docker compose
参考:Docker相关
安装
选择任意目录下创建文件夹nginx
在nginx文件夹下创建conf、log、html、certificate目录
在conf文件夹下创建conf.d文件夹和nginx.conf文件
在conf.d文件夹下创建default.conf文件
nginx.conf 和 default.conf (nginx.config为自己定义的配置文件,按需修改)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| # 这是nginx默认的配置文件,按需修改 server { listen 80; listen [::]:80; server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / { root /usr/share/nginx/html; index index.html index.htm; }
#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; #} }
|
在nginx文件夹下创建docker-compose.yaml文件
docker-compose.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| services: nginx: image: nginx:1.25.4 restart: unless-stopped container_name: nginx network_mode: "host" volumes: - ./conf/nginx.conf:/etc/nginx/nginx.conf - ./conf/conf.d:/etc/nginx/conf.d - ./log:/var/log/nginx - ./html:/usr/share/nginx/html - ./certificate:/certificate
|
启动
docker-compose.yaml文件配置好后启动容器
其他
常用配置示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| worker_processes 1;
events { worker_connections 1024; }
http { include mime.types; default_type application/octet-stream;
client_max_body_size 64m;
sendfile on;
keepalive_timeout 65; server { listen 80; listen [::]:80; server_name xxx.com;
location = / { rewrite ^/(.*) https://xxx.com/$1 permanent; } location / { rewrite ^/(.*) https://xxx.com/ permanent; } } server { listen 443 ssl; listen [::]:443 ssl; server_name xxx.com; ssl_session_timeout 5m; ssl_certificate /certificate/xxx.com.pem; ssl_certificate_key /certificate/xxx.com.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; ssl_prefer_server_ciphers on;
location /redirectUrl { proxy_redirect off; proxy_pass http://ip:port; proxy_http_version 1.1; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } }
|
代理websocket的设置
以minio-console为例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| server { listen 80; listen [::]:80; # 域名 server_name minio.xxx.com; location / { proxy_pass http://ip:port; proxy_set_header HOST $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /ws/ { proxy_pass http://ip:port; proxy_http_version 1.1; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 支持websocket proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } }
|