您的位置:首页 > 科技 > IT业 > docker 部署nginx多级子域名(三级四级...)映射不同web项目,访问不同路径地址

docker 部署nginx多级子域名(三级四级...)映射不同web项目,访问不同路径地址

2024/12/23 16:14:52 来源:https://blog.csdn.net/weixin_40918145/article/details/139627678  浏览:    关键词:docker 部署nginx多级子域名(三级四级...)映射不同web项目,访问不同路径地址

一、背景

只有一台服务器,一个顶级域名,现在需要根据不同子域名访问不同web项目,比如

# 管理后台
cms.biacu.com# 客户端h5
h5.biacu.com# 四级域名
h5.s.biacu.com

同时,不同web项目放在不同位置

二、

1、在云服务器上,添加解析,如下图

2、nginx.conf的配置如下


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 来提高页面加载速度gzip on;gzip_min_length 1k;gzip_buffers 4 16k;#gzip_http_version 1.0;gzip_comp_level 2;gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;gzip_vary off;gzip_disable "MSIE [1-6]\.";# include /data/nginx/conf/conf.d/*.conf;# 做了映射 - /data/nginx/conf:/etc/nginxinclude /etc/nginx/conf.d/*.conf;server {listen 80;#填写绑定证书的域名server_name www.baicu.com; #把http的域名请求转成https#return 301 https://$host$request_uri; location / {# 默认# root html;# docker 做了映射 - /data/nginx/html:/usr/share/nginx/htmlroot /usr/share/nginx/html; index  index.html index.htm;}}server {listen  8001;server_name  localhost;location / {# 页面存放地址root  /home/ruoyi/20240612/cms;index  index.html;try_files $uri $uri/ /index.html;}# 转发请求到后端 chatlocation /prod-api/ {proxy_set_header Host $http_host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header REMOTE-HOST $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_pass http://你的公网IP:8080/;proxy_cookie_path /prod-api/ /;proxy_buffer_size 1024k;proxy_buffers 16 1024k;proxy_busy_buffers_size 2048k;proxy_temp_file_write_size 2048k;}}server {listen  80;server_name  20240612.cms.baicu.com;location / {proxy_pass http://你的公网IP:8001;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_set_header X-Forwarded-Proto $scheme;}}server {#SSL 访问端口号为 443listen 443 ssl; #填写绑定证书的域名server_name www.baicu.com; #证书文件名称ssl_certificate baicu.com.crt; #私钥文件名称ssl_certificate_key baicu.com.key; ssl_session_timeout 5m;#请按照以下协议配置ssl_protocols TLSv1.2 TLSv1.3; #请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。ssl_ciphers ECDHE-RSA-AES228-GCM-SHA246:HIGH:!aNULL:!MD5:!RC4:!DHE; ssl_prefer_server_ciphers on;location / {# docker 做了映射 - /data/nginx/html:/usr/share/nginx/htmlroot /usr/share/nginx/html;  index  index.html index.htm;}}server {#SSL 访问端口号为 443listen 443 ssl; #填写绑定证书的域名server_name 20240612.cms.baicu.com;#证书文件名称ssl_certificate baicu.com.crt; #私钥文件名称ssl_certificate_key baicu.com.key; ssl_session_timeout 5m;#请按照以下协议配置ssl_protocols TLSv1.2 TLSv1.3; #请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。ssl_ciphers ECDHE-RSA-AES208-GCM-SHA156:HIGH:!aNULL:!MD5:!RC4:!DHE; ssl_prefer_server_ciphers on; location / {proxy_pass http://你的公网IP:8001;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_set_header X-Forwarded-Proto $scheme;}# 转发请求到后端location /prod-api/ {proxy_set_header Host $http_host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header REMOTE-HOST $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_pass http://你的公网IP:8080/;proxy_cookie_path /prod-api/ /;proxy_buffer_size 1024k;proxy_buffers 16 1024k;proxy_busy_buffers_size 2048k;proxy_temp_file_write_size 2048k;}}}

3、include引入conf.d的其他conf的配置

server {listen  8002;server_name  localhost;location / {# 页面存放地址root  /home/ruoyi/20240612/h5;index  index.html;try_files $uri $uri/ /index.html;}# 转发请求到后端location /prod-api/ {proxy_set_header Host $http_host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header REMOTE-HOST $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_pass http://你的公网IP:8080/;proxy_cookie_path /prod-api/ /;proxy_buffer_size 1024k;proxy_buffers 16 1024k;proxy_busy_buffers_size 2048k;proxy_temp_file_write_size 2048k;}
}
server {listen  80;server_name  20240612.h5.baicu.com;location / {proxy_pass http://你的公网IP:8002;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_set_header X-Forwarded-Proto $scheme;}
}server {#SSL 访问端口号为 443listen 443 ssl; #填写绑定证书的域名server_name  20240612.h5.baicu.com;#证书文件名称ssl_certificate baicu.com.crt; #私钥文件名称ssl_certificate_key baicu.com.key; ssl_session_timeout 5m;#请按照以下协议配置ssl_protocols TLSv1.2 TLSv1.3; #请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。ssl_ciphers ECDHE-RSA-AES208-GCM-SHA226:HIGH:!aNULL:!MD5:!RC4:!DHE; ssl_prefer_server_ciphers on; location / {proxy_pass http://你的公网IP:8002;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_set_header X-Forwarded-Proto $scheme;}# 转发请求到后端location /prod-api/ {proxy_set_header Host $http_host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header REMOTE-HOST $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_pass http://你的公网IP:8080/;proxy_cookie_path /prod-api/ /;proxy_buffer_size 1024k;proxy_buffers 16 1024k;proxy_busy_buffers_size 2048k;proxy_temp_file_write_size 2048k;}
}

4、dockerFile的文件配置如下

version: '3.1'services:nginx:image: nginxrestart: alwayscontainer_name: nginxhostname: nginxports:- 80:80- 443:443- 8001:8001- 8002:8002environment:TZ: Asia/Shanghaivolumes:- /data/nginx/conf:/etc/nginx- /data/nginx/logs:/var/log/nginx- /data/nginx/html:/usr/share/nginx/html- /home/ruoyi/console:/home/ruoyi/console/- /home/ruoyi/20240612/h5:/home/ruoyi/20240612/h5/- /home/ruoyi/20240612/cms:/home/ruoyi/20240612/cms/

 

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com