关于nginx:Nginx配置

2次阅读

共计 2289 个字符,预计需要花费 6 分钟才能阅读完成。

nginx 配置样板

http {
    # ...
    server {
        # 监听端口号
        listen 443;
        server_name xxxx;
        loaction / {root 文件目录}
    }
}

注: 需确保服务器防火墙和平安组都放通拜访端口

部署 webpack 打包 publicPath 为 ‘/‘ 的 dist

  • 拜访: http://IP:port
location / {
        root C:\Users\Administrator\Desktop\vue-project\emss\dist;
        index index.html ;
        try_files $uri $uri/ index.html;
}

注: 如果前端路由模式为 history, 则必须加 try_files $uri $uri/ index.html;

部署 webpack 打包 publicPath 为 ’/app'(即蕴含自定义后缀) 的 dist

  • 拜访: http://IP:port/app
location /app {alias C:\Users\Administrator\Desktop\vue-project\app\dist;}

注: 此时如果前端我的项目中须要拜访 public 目录下的文件, 拜访门路应改为: /app/public 目录下的文件

后端 serve 转发 (一)

  • 后端服务运行在 30000 端口上, api 路由为: /api/v1/xxxx
  • 拜访 api: http://IP:port/api/v1/xxxx
location /api {
        proxy_pass http://127.0.0.1:30000/api;
        # 或者 proxy_pass http://127.0.0.1:30000
        }

注: 如果仅写到端口号, 则端口号后不加 /

后端 serve 转发 (二)

  • 后端服务运行在 8803 端口上, api 路由为: /api/v1/xxxx
  • 拜访 api: http://IP:port/dmt/api/v1/xxxx
location /dmt/api {proxy_pass http://127.0.0.1:8803/api;}

后端 serve 转发 (三)

  • 后端服务运行在 30003 端口上, api 路由为: /aa/bb/xxx
  • 拜访 api: http://IP:port/chinaTalk/aa/b…
location /chinaTalk/ {rewrite ^/chinaTalk/(.*)$ /$1 break;
        proxy_pass http://119.45.102.83:30003;
}

转发文件资源拜访

  • 后端 serve 运行在 30000 端口下, 后端启动动态资源在 uploads 目录下
  • 拜访动态资源: http://IP:port/uploads/ 文件名
location /uploads {proxy_pass http://127.0.0.1:30000/uploads;}

应用 https 域名替换 ip 端口拜访

  • https 必须在 443 端口下
  • 须要拿到 https 证书文件并放在 nginx-1.xx.xx/conf 目录下 (即 nginx.conf 所在目录)
server {
       #SSL 拜访端口号为 443
       listen 443 ssl;
       #填写绑定证书的域名
       server_name wzctest.wzc520pyf.cn;
       #证书文件名称
       ssl_certificate 1_wzctest.wzc520pyf.cn_bundle.crt;
       #私钥文件名称
       ssl_certificate_key 2_wzctest.wzc520pyf.cn.key;
       ssl_session_timeout 5m;
       #请依照以下协定配置
       ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
       #请依照以下套件配置,配置加密套件,写法遵循 openssl 规范。ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
       ssl_prefer_server_ciphers on;
       location / {
            root C:\Users\Administrator\Desktop\vue-project\emss\dist;
            index index.html ;
            try_files $uri $uri/ index.html;
       }
}

开启前端打包 gz 压缩反对

server {
           listen       3333;
           server_name  localhost;
           # compression-webpack-plugin 配置
    
           gzip on;
    
        gzip_min_length 1k;
    
        gzip_comp_level 9;
    
        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 on;
    
        # 配置禁用 gzip 条件,反对正则,此处示意 ie6 及以下不启用 gzip(因为 ie 低版本不反对)gzip_disable "MSIE [1-6]\.";

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root  C:\Users\Administrator\Desktop\vue-project\emss\dist;
            try_files $uri $uri/ @router;
            index  index.html index.htm;
        }
        location @router {rewrite ^.*$ /index.html last;}
}

配置属性

正文完
 0