nginx配置样板

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

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

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

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

location /app {
         alias C:\Users\Administrator\Desktop\vue-project\app\dist;
}

注: 此时如果前端项目中需要访问public目录下的文件, 访问路径应改为: /app/public目录下的文件

后端serve转发(一)

location /api {
        proxy_pass http://127.0.0.1:30000/api;
        # 或者proxy_pass http://127.0.0.1:30000
        }

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

后端serve转发(二)

location /dmt/api {
        proxy_pass http://127.0.0.1:8803/api;
}

后端serve转发(三)

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;
        }
}

多节点部署

  • 服务更新, 从更新到新服务启动会有一段时间间隔,这段间隔里是访问不到服务的, 这很糟, 解决办法就是多节点部署
  • 访问: https://域名//project/api

    http {
        # ...
            #  max_fails为允许失败的最大次数,fail_timeout为重连间隔
        upstream project-lbs {
            server 127.0.0.1:8803 max_fails=1 fail_timeout=60s;
            server 127.0.0.1:8804 max_fails=1 fail_timeout=60s;
        }
        server {
            # 省去配置https的步骤
            location /project/api {
                proxy_pass http://project-lbs/api;
            }
        }
    }
  • 同时将服务部署在8803和8804两个端口, 当其中一个端口服务不可用时, nginx就会尝试另一个端口

负载均衡

负载均衡有多种分发方式, 下面介绍个人了解的几种

  • nginx默认的节点轮询

    # 用户的请求将按序被分配到两个节点, 如果有两个请求,则一个节点一个
    upstream project-lbs {
        server 127.0.0.1:8803;
        server 127.0.0.1:8804;
    }
  • weight权重配置

    # 占比量, weight越高, 分配的流量就越高
    upstream project-lbs {
        server 127.0.0.1:8803 weight=5;
        server 127.0.0.1:8804 weight=10;
    }
  • ip_hash固定分发

    # 根据用户ip进行哈希,依据结果分配到对应节点,现在每个用户可以固定访问到我们的某一个节点
    upstream project-lbs {
        ip_hash;
        server 127.0.0.1:8803 weight=5;
        server 127.0.0.1:8804 weight=10;
    }
  • 其他配置

    # down表示当前server咱不参与负载
    upstream project-lbs {
        server 127.0.0.1:8803 down;
        server 127.0.0.1:8804;
    }
    # backup表示: 当其他所有非backupp的机器down的时候,会请求backup的机器,因此这台机器压力会最轻,配置也相应地可以低一些
    upstream project-lbs {
        server 127.0.0.1:8803 backup;
        server 127.0.0.1:8804;
    }

配置属性


lalala
3 声望3 粉丝

时间多反而容易使人懒散,缺乏动力,效率低下.