docker的nginx配置过程

启动

docker run -d --name nginx --privileged -p 8000:8000 -p 8091:8091 \
    -v /docker/nginx/html/ruoyi-ui:/usr/share/nginx/html/ruoyi-ui \
    -v /docker/nginx/html/samr-ui:/usr/share/nginx/html/samr-ui \
    -v /home/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /home/nginx/logs:/var/log/nginx \
    nginx

nginx.conf

worker_processes 1;

events {
    worker_connections 1024;
}

http {
    include mime.types;
    default_type application/octet-stream;
    client_max_body_size 100M;
    sendfile on;
    keepalive_timeout 65;

    server {
        listen 80;
        server_name localhost;
        charset utf-8;

        location / {
            root /usr/share/nginx/html/ruoyi-ui;
            try_files $uri $uri/ /index.html;
            index index.html index.htm;
        }

        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://172.16.82.75:8080/;
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root html;
        }
    }

    server {
        listen 8091;
        server_name localhost;
        charset utf-8;

        location /samr {
            root /usr/share/nginx/html/samr-ui;
            try_files $uri $uri/ @router;
            index index.html index.htm;
        }

        root /usr/share/nginx/html/samr-ui;

        location / {
            #指向下面的@router否则会出现vue路由在nginx中刷新出现404
            try_files $uri $uri/ @router;
            index index.html;
        }

        #对应上面的@router,主要原因是路由的路径资源并不是一个真实的路径,所以无法找到具体的文件
        #因此需要rewrite到index.html中,然后交给路由在处理请求资源
        location @router {
            rewrite ^.*$ /index.html last;
        }

        #error_page  404              /404.html;

        # (必须)为css和js文件的请求添加请求头,防止出现response的contentType为text/html,浏览器按照html解析然后报错
        location ~ \.css {
            add_header Content-Type text/css;
        }
        location ~ \.js {
            add_header Content-Type application/x-javascript;
        }
        # (必须)设置html不进行缓存
        location ~ \.html {
            add_header Cache-Control no-cache;
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root html;
        }
    }
}

新问题

docker run -d --name nginx --privileged -p 80:80 -p 8000:8000 -p 8091:8091 -p 8095:8095 \
    -v /docker/nginx/html/ruoyi-ui:/usr/share/nginx/html/ruoyi-ui \
    -v /docker/nginx/html/samr-ui:/usr/share/nginx/html/samr-ui \
    -v /docker/nginx/html/quality-ui:/usr/share/nginx/html/quality-ui \
    -v /home/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /home/nginx/logs:/var/log/nginx \
    nginx
worker_processes 1;

events {
    worker_connections 1024;
}

http {
    include mime.types;
    default_type application/octet-stream;
    client_max_body_size 100M;
    sendfile on;
    keepalive_timeout 65;
    server {
        listen 8000;
        server_name localhost;
        charset utf-8;
        location / {
            root /usr/share/nginx/html/ruoyi-ui;
            try_files $uri $uri/ /index.html;
            index index.html index.htm;
        }
        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://172.16.82.75:8080/;
        }
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root html;
        }
    }

    server {
        listen 8091;
        server_name localhost;
        charset utf-8;
        location /samr {
            root /usr/share/nginx/html/samr-ui;
            try_files $uri $uri/ @router;
            index index.html index.htm;
        }
        root /usr/share/nginx/html/samr-ui;
        location / {
            try_files $uri $uri/ @router;
            index index.html;
        }
        location @router {
            rewrite ^.*$ /index.html last;
        }

        #error_page  404              /404.html;
        location ~ \.css {
            add_header Content-Type text/css;
        }
        location ~ \.js {
            add_header Content-Type application/x-javascript;
        }
        location ~ \.html {
            add_header Cache-Control no-cache;
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root html;
        }
    }

    server {
        listen 8095;
        server_name localhost;
        charset utf-8;
        location /samr {
            root /usr/share/nginx/html/quality-ui;
            try_files $uri $uri/ @router;
            index index.html index.htm;
        }
        root /usr/share/nginx/html/quality-ui;
        location / {
            try_files $uri $uri/ @router;
            index index.html;
        }
        location @router {
            rewrite ^.*$ /index.html last;
        }

        #error_page  404              /404.html;
        location ~ \.css {
            add_header Content-Type text/css;
        }
        location ~ \.js {
            add_header Content-Type application/x-javascript;
        }
        location ~ \.html {
            add_header Cache-Control no-cache;
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root html;
        }
    }
}
暂无评论

发送评论 编辑评论


				
上一篇
下一篇