nginx常用配置

nginx配置IE內核跳轉


   if ( $http_user_agent ~* "MSIE [6-8].[0-9]" ) {

        rewrite http://test.10010.com/a3 break;

    }

    if ( $http_user_agent ~* "MSIE 9|1[0-2].[0-9]" ) {

        rewrite http://test.10010.com/a4 break;

    }


nginx實現跨域訪問    


http {
  ......
  add_header Access-Control-Allow-Origin *;
  add_header Access-Control-Allow-Headers X-Requested-With;
  add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
}


這樣就可以實現GET,POST,OPTIONS的跨域請求的支持
也可以 add_header Access-Control-Allow-Origin http://test.51testing.com; --指定允許的url;


nginx增加用戶名密碼認證配置


生產用戶認證的用戶名和密碼:

htpasswd -c passwd_kb admin

更改nginx配置文件

location ~ /wapService-test1/* {

                        auth_basic "Authorized users only";   #提示

                        auth_basic_user_file /opt/nginx/conf/password_kb; #路徑

                        proxy_set_header   Host             $host;

                        proxy_set_header   X-Real-IP        $remote_addr;

                        proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;

                        proxy_redirect         off;

                        proxy_set_header       Authorization ""; #避免代理weblogic重複提示認證問題

                        proxy_set_header       X-Forwarded-Ssl on;

                        proxy_pass http://all;


重啓nginx,輸入URL進行測試

wKiom1d9vi2hulwgAACMzdfMBgQ142.png-wh_50


nginx POST請求靜態文件提示405錯誤問題


方法一:

error_page   405 =200 @405;
                location @405{
                root html;
                proxy_method GET;
                proxy_pass 
http://localhost:80;
                }

方法二:

vim ngx_http_static_module.c
找到如下行(大約在文件的第206行):

if (r->method & NGX_HTTP_POST) {
     return NGX_HTTP_NOT_ALLOWED;
}

將這段屏蔽掉;
/*
if (r->method & NGX_HTTP_POST) {
     return NGX_HTTP_NOT_ALLOWED;
}
*/


保存退出;

(方法二來自網絡,未經測試)
在使用之前編譯nginx的參數,重新編譯nginx版本,並進行替換(注意不要make install)即可。


nginx設置不緩存文件


location ~ .*.(css|js|swf|php|htm|html )$ {  

     ........

    add_header Cache-Control no-store;  

}


nginx根據cookie跳轉


需求:

/smart/ 這個請求如果cookie中包含test=true 則轉發到預發佈節點,如果不包含轉發到生產節點。

/smartplatform/ 這個請求如果cookie中包含test=true 則轉發到預發佈節點,如果不包含轉發到生產節點。


定義upstream

 upstream houtai {

        sticky;

        server  10.70.52.11:9110 max_fails=2 fail_timeout=10s;

        server  10.70.52.11:9111 max_fails=2 fail_timeout=10s;

    }

    upstream push {

        server 10.70.52.11:9110;

    }

定義cookie匹配

    map $COOKIE_test $my_upstream {

    ~^true$ push;

    default houtai;

    }

配置location

       location ^~ /smart/ {

            proxy_pass      http://$my_upstream;

            proxy_buffer_size 128k;

            proxy_buffers   32 128k;

            proxy_set_header Host $host;

            proxy_set_header X-Real-IP $remote_addr;

            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章