nginx域名跳轉技巧

1.地址重寫:訪問server_name的時候跳轉到http://www.cnblogs.com/qinyujie/linux

修改nginx配置文件。加入到server{...}字段或者location字段裏面:使用rewrite301跳轉到指定的地址。nginx

listen 80;
server_name www.qinyujie.com;web

rewrite ^/(.*)$ http://www.cnblogs.com/qinyujie/$1 last;後端

 2.企業安全,nginx禁止使用ip訪問:瀏覽器

修改nginx配置文件:安全

http:{...}中加入以下字段:服務器

server  {  session

listen 80 default;負載均衡

 return 500;ide

 }

 

3.nginx配置反向代理  

   location / {
                  proxy_set_header Host $host;                    #修改發送到後端的header的host
                  proxy_set_header X-Real-Ip $remote_addr;                 #設置真實ip
                  proxy_set_header X-Forwarded-For $remote_addr;   
                  proxy_pass http://192.168.0.162:80;                   #後端ip地址
         }

 

4.nginx的http負載均衡模塊:在http:{...}字段中配置;'

upstream linuxidc { #定義負載均衡設備的Ip及設備狀態 

 ip_hash;  #session會話保持
      server xxxx:7080 down; 
      server xxxx:8980 weight=7;

    server xxxx:9999  backup; 

}

在location / {...}中加入「‘   proxy_pass http://linuxidc; ’」便可。

   ip_hash(訪問ip)

    每一個請求按訪問ip的hash結果分配,這樣每一個訪客固定訪問一個後端服務器,能夠解決session的問題。

upstream還能夠爲每一個設備設置狀態值,這些狀態值的含義分別以下:

down 表示單前的server暫時不參與負載.

weight 默認爲1.weight越大,負載的權重就越大。

max_fails :容許請求失敗的次數默認爲1.當超過最大次數時,返回proxy_next_upstream 模塊定義的錯誤.

fail_timeout : max_fails次失敗後,暫停的時間。

backup: 其它全部的非backup機器down或者忙的時候,請求backup機器。因此這臺機器壓力會最輕。

 

5.官網一級域名跳轉(301重定向)

 

需求:在IE瀏覽器下打開9wee.com直接跳轉到www.qinyj.top,方便旗下游戲內容獲取

 

修改nginx配置文件在,root後添加

 

        if ($host =  "qinyj.top")
        {
                rewrite ^(.*)   http://www.qinyj.top$1 permanent;

    # return 403;   #可根據遠程ip拒絕某個頻繁訪問的ip,
        }

 

 if ($http_user_agent ~* "qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot")
{
return 403;        #    if條件語句能夠控制url的跳轉。可防治爬蟲。防搜索引擎的
}

重啓/etc/init.d/nginx reload 便可

 

6.nginx訪問控制

nginx中內置ngx_http_access_module

加入到location 生效區域內:

  location / {
  deny 192.168.1.1;
  allow 192.168.1.0/24;
  allow 10.1.1.0/16;
  allow 2001:0db8::/32;
  deny all;
  }
重啓nginx便可。
相關文章
相關標籤/搜索