uWSGI 是在像 nginx 、 lighttpd 以及 cherokee 服務器上的一個部署的選擇。更多選擇見 FastCGI 和 獨立 WSGI 容器 。 你會首先需要一個 uWSGI 服務器來用 uWSGI 協議來使用你的 WSGI 應用。 uWSGI 是一個協議,同樣也是一個應用服務器,可以提供 uWSGI 、FastCGI 和 HTTP 協議。
1、使uwsgi服務器響應代碼大于或等于300的響應重定向到nginx以使用error_page指令進行處理
uwsgi_intercept_errors on;
2、nginx簡單過濾爬蟲
#禁止爬蟲工具的抓取
if ($http_user_agent ——* "python|curl|java|wget|httpclient|okhttp|Scrapy") {
return 503;
}
#禁止指定UA及UA為空的訪問
if ($http_user_agent —— "WinHttp|WebZIP|FetchURL|node-superagent|java/|FeedDemon|Jullo|JikeSpider|Indy Library|Alexa Toolbar|AskTbFXTV|AhrefsBot|CrawlDaddy|Java|Feedly|Apache-HttpAsyncClient|UniversalFeedParser|ApacheBench|Microsoft URL Control|Swiftbot|ZmEu|oBot|jaunty|Python-urllib|lightDeckReports Bot|YYSpider|DigExt|HttpClient|MJ12bot|heritrix|EasouSpider|Ezooms|BOT/0.1|YandexBot|FlightDeckReports|Linguee Bot|^$" ) {
return 403;
}
3、http請求重定向到https
#http跳轉https
set $flag 0;
if ($host = "wxapp.zyqcn.cn") {
set $flag "${flag}1";
}
if ($scheme = "http") {
set $flag "${flag}2";
}
if ($flag = "012") {
rewrite ^(.*) https://$host$1 permanent;
}
4、將錯誤頁狀態碼重設為200,并返回指定內容
error_page 502 404 405 500 =200 /error;
#error最好不要帶后綴,之前寫了個error.html,然后下面想返回成json,結果各種設置不起作用,后來搞了半天之后才發現是后綴的鍋
location /error {
default_type application/json;
#add_header name value always;#always是可選參數,已經存在這個header的情況下使用不會覆蓋
add_header Access-Control-Allow-Origin *;
return 200 '{"code": 0,"msg":"您的請求暫時無法處理","more": $status}';
}