nginx 相關

模擬if多重判斷

背景:由於有人惡意刷咱們一個連接,拒掉這些訪問javascript

僞代碼以下:css

if ( $request ~ "/credit-user/reg-get-code"  && $http_user_agent ~ okhttp ) { 
rewrite
^/(.*)$ https://www.iteblog.com/$1 permanent;
#return 403;
   }

但惋惜nginx並不支持這種寫法,只好用下面這種寫法進行多重判斷,經測試可用。html

    location / {
        proxy_pass  http://127.0.0.1:8080;
        set $flag 0;
        if ( $request ~ "/credit-user/reg-get-code"  ) {
             set $flag "${flag}1";
           }
        if ( $http_user_agent ~ okhttp ) {
             set $flag "${flag}2";
           }
        if ( $flag = "012" ) {
            return 403;
        }
      
}

驗證結果:vue

 

 

 

域名重定向

好比訪問  http://***.com/abc/api/jd/id=1  指向 http://***.com/api/jd/id=1 java

location ~^/abc/ {
        rewrite /abc/(.*) /$1 break;
  }

 

 

配置 vue  history路由

一、根路徑配置nginx

server {
    listen              80;
    server_name         vue.xxx.com;

location  / {
        index  index.html index.htm;
        root   /var/www/channelGift/;  #vue項目路徑
        try_files $uri $uri/ /index.html;
    }

}

 

二、非根目錄配置api

server {
    listen              80;
    server_name         a.xxx.com;   #a域名

location / {
        index  index.html index.htm;
        root   /var/www/a;    #a域名根目錄
        error_page   500 502 503 504 /index.html;
    }
location  ^~ /channelGift {
        alias   /var/www/b/;  #vue項目路徑
        index  index.html;
        try_files $uri $uri/ /channelGift/index.html;   #不要用絕對路徑,如 /var/www/channelGift/index.html
    }
}

 

還應該在VUE項目裏把每一個路徑加上[/channelGift]這一段(或者指定base: '/channelGift/'),要不頁面會顯示爲空白:app

 

 

 

訪問時 使用下面路徑便可訪問curl

http://a.xxx.com/channelGift/product-list.html?fcode=1a399419

 

 

開啓js壓縮

修改NGINX參數,開啓gzip,壓縮類型gzip_types中的application/x-javascript並非表明javascript,真正表明javascript的是:application/javascript,因此還須要在gzip_types中添加進去。而後重啓NGINX測試

 http {
gzip
on; gzip_min_length 10k; gzip_comp_level 6; gzip_static on; gzip_types text/html text/plain application/x-javascript application/javascript text/css application/xml text/javascript image/jpeg image/gif image/png;
}

測試

若是返回結果有Content-Encoding: gzip 說明壓縮成功

[root@online-01 nginx]# curl -I -H "Accept-Encoding: gzip, deflate" "http://www.*****.com/css/chunk-vendors.728eb7d9.css"
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Sat, 12 Oct 2019 09:54:39 GMT
Content-Type: text/css
Last-Modified: Fri, 11 Oct 2019 11:43:45 GMT
Connection: keep-alive
ETag: W/"5da06af1-39032"
Expires: Tue, 22 Oct 2019 09:54:39 GMT
Cache-Control: max-age=864000
Content-Encoding: gzip

 

或者在開發者控制檯裏查看,response headers 有以下內容就是開啓了gzip,

沒有開啓gzip,則在request headers 裏顯示Accept-Encoding:gzip, deflate

相關文章
相關標籤/搜索