当前位置: 移动技术网 > 网络运营>服务器>nginx > 关于nginx+uWsgi配置遇到的问题的解决

关于nginx+uWsgi配置遇到的问题的解决

2019年04月18日  | 移动技术网网络运营  | 我要评论

写字姿势,漂泊在异界的日子,感动中国赵红霞

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}';
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网