美文网首页
nginx操作

nginx操作

作者: KC莲 | 来源:发表于2021-03-30 15:53 被阅读0次

    启动nginx
    start nginx

    重启nginx
    nginx -s reload

    关闭nginx
    nginx -s quit

    开启gzip

    gzip on;
    gzip_min_length  5k;
    gzip_buffers     4 16k;
    #gzip_http_version 1.0;
    gzip_comp_level 3;
    gzip_types       text/plain application/javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
    gzip_vary on;
    

    配置跨域

    server {
            listen       80;
            server_name  localhost;
    
            location /vfs {
                proxy_pass http://192.168.20.57:8082;
            }
           如果遇到需要统一转发并自行添加统一路径的情况,需要在转发后重url
           location /dev-api {
                rewrite  ^.+dev-api/?(.*)$ /$1 break;
                proxy_pass http://192.168.20.57:8082;
            }
        }
    

    兼容history路由

    location / {
                try_files $uri $uri/ /index.html;
            }
    

    重写统一转发路径

    location /prod-api {
               rewrite  ^.+prod-api/?(.*)$ /$1 break;
               proxy_pass http://127.0.0.1:8080;
            }
    

    配置文件参考

    worker_processes  1;
    
    events {
        worker_connections  1024;
    }
    
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        sendfile        on;
    
        keepalive_timeout  65;
    
        # 开启|关闭 gzip    
        gzip on;
        # 文件大于指定 size 才压缩,以 kb 为单位
        gzip_min_length  5k;
        # 请求压缩的缓冲区数量和大小,以 4k 为单位,32 为倍数
        gzip_buffers     4 16k;
        #gzip_http_version 1.0;
        # 压缩级别,1-9,值越大压缩比越大,但更加占用 CPU,且压缩效率越来越低
        gzip_comp_level 3;
        # 压缩的文件类型。
        gzip_types       text/plain application/javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
        # 开启后如果能找到 .gz 文件,直接返回该文件,不会启用服务端压缩
        gzip_static on;  
        # 是否添加响应头 Vary: Accept-Encoding 建议开启
        gzip_vary on;
    
        #server:根据端口来创建服务
        server {
            listen       8081;
            server_name  localhost;#域名配置 如有需要
    
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
            
            #location :根据当前server端口下指定路径来创建服务
            location / {
                root   html;#root:服务的根目录
                index  index.html index.htm;#index:入口文件
                try_files $uri $uri/ /insure-web/index.html;history路由兼容
            }
    
            location /fms-mobile {#子路由配置
               alias   html/fms-mobile;
                index  index.html index.htm;
           }
    
           location /prod-api {
               rewrite  ^.+prod-api/?(.*)$ /$1 break;#代理路径重写
               proxy_pass http://127.0.0.1:8080;#代理配置
            }
    
            error_page  404              /404.html; #404页面指定
    
            # redirect server error pages to the static page /50x.html
            #
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
    
        server {
            listen 8082;
            server_name  localhost;
    
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            location / {
                root   html/prod-fms;
                index  index.html index.htm;
            }
    
            #error_page  404              /404.html;
    
            # redirect server error pages to the static page /50x.html
            #
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
    
        #https配置
        server {
            listen 8083 ssl;
            server_name  xxxx.com;
    
            ssl_certificate       ../cert/xxxx.pem;
            ssl_certificate_key   ../cert/xxxx.key;
    
            ssl_session_cache    shared:SSL:1m;
            ssl_session_timeout  5m;
    
            ssl_ciphers  ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
            ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
            ssl_prefer_server_ciphers  on;
    
           location / {
               proxy_pass http://127.0.0.1:8080;
            }
    
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
    
        #开放443端口给部分需要验证证书的服务访问
        server {
             listen       443 ssl;
             server_name  xxxx.com; 
     
            #https证书配置
            ssl_certificate       ../cert/xxxx.pem;#pem文件位置
            ssl_certificate_key   ../cert/xxx.key;#key文件位置
    
            ssl_session_cache    shared:SSL:1m;
            ssl_session_timeout  5m;
    
            ssl_ciphers  ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
            ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
            ssl_prefer_server_ciphers  on;
    
            location / {
                root   html;
                index  index.html index.htm;
            }
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:nginx操作

          本文链接:https://www.haomeiwen.com/subject/earghltx.html