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/nginx
    include /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/html
            root /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;
        }
        
        # 转发请求到后端 chat
        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.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 访问端口号为 443
        listen 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/html
           root /usr/share/nginx/html;  
           index  index.html index.htm;
        }
    }
    
    
    server {
        #SSL 访问端口号为 443
        listen 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 访问端口号为 443
    listen 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: nginx
    restart: always
    container_name: nginx
    hostname: nginx
    ports:
      - 80:80
      - 443:443
      - 8001:8001
      - 8002:8002
    environment:
      TZ: Asia/Shanghai
    volumes:
      - /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/

 

 

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>