我有一个安装了 SSL 证书的 nginx 服务器。我想将所有请求上游传递到运行于 的 Gunicorn 服务器0.0.0.0:8000
。但是,每当我运行 Gunicorn 服务器时,它都会出现错误,提示重定向循环过多。如果我通过 https 运行 gunicorn,则连接将变得安全,但它不会连接到 gunicorn 服务器,只会显示bad gateway
。此外,这是我在使用 https 运行 gunicorn 时尝试连接时收到的错误:
Traceback (most recent call last):
File "/opt/bitnami/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 515, in spawn_worker
worker.init_process()
File "/opt/bitnami/python/lib/python2.7/site-packages/gunicorn/workers/base.py", line 126, in init_process
self.run()
File "/opt/bitnami/python/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 119, in run
self.run_for_one(timeout)
File "/opt/bitnami/python/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 66, in run_for_one
self.accept(listener)
File "/opt/bitnami/python/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 30, in accept
self.handle(listener, client, addr)
File "/opt/bitnami/python/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 141, in handle
self.handle_error(req, client, addr, e)
File "/opt/bitnami/python/lib/python2.7/site-packages/gunicorn/workers/base.py", line 213, in handle_error
self.log.exception("Error handling request %s", req.uri)
AttributeError: 'NoneType' object has no attribute 'uri'
[2016-01-01 15:37:45 +0000] [935] [INFO] Worker exiting (pid: 935)
[2016-01-01 20:37:45 +0000] [938] [INFO] Booting worker with pid: 938
[2016-01-01 15:37:46 +0000] [938] [ERROR] Exception in worker process:
这是我的 nginx 配置:
server {
# port to listen on. Can also be set to an IP:PORT
listen 80;
listen 443 ssl;
ssl_certificate /etc/ssl/pyhub_crt.crt;
ssl_certificate_key /etc/ssl/pyhub.key;
server_name www.pyhub.co;
server_name_in_redirect off;
access_log /opt/bitnami/nginx/logs/access.log;
error_log /opt/bitnami/nginx/logs/error.log;
location /E0130777F7D5B855A4C5DEB138808515.txt {
root /home/bitnami;
}
location / {
proxy_pass_header Server;
proxy_set_header Host $host;
proxy_set_header X-Scheme $scheme;
proxy_set_header X-SSL-Protocal $ssl_protocol;
proxy_connect_timeout 10;
proxy_read_timeout 10;
proxy_redirect http:// $scheme://;
proxy_pass http://localhost:8000;
}
答案1
如果 gunicorn 绑定到 0.0.0.0,则表示它已绑定到所有接口,因此它已暴露给外部接口。如果 nginx 尝试绑定到同一端口上的任何接口,则将失败。
Gunicorn 应该绑定到特定的 ip 或者更好的 127.0.0.1,因此它只绑定到内部 ip。
第二,你说你想将 https 传递给 gunicorn,但到你拥有证书的代理(即 ngninx)的流量受到 SSL 保护。此后,除非你在 gunicorn 上也设置了 SSL,否则流量在内部(即 http)到 gunicorn 是明文的。
因此,你的 nginx 配置应该包含:
- gunicorn 的上游服务器,ip 为 127.0.0.1,端口为 8080,或任何您想要的。
- 服务器指令监听端口 80(用于 http)和 443(用于 https)
- 服务器块中的代理传递指令用于转发到上游
我的 nginx SSL 代理配置如下:
upstream website {
ip_hash; # for sticky sessions, more below
server website:8000 max_fails=1 fail_timeout=10s;
}
server {
# only listen to https here
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name yourdomain.here.com;
access_log /var/log/nginx/yourdomain.here.com.access.log;
error_log /var/log/nginx/yourdomain.here.com.error.log;
ssl on;
ssl_certificate /etc/nginx/certs/ca-cert.chained.crt;
ssl_certificate_key /etc/nginx/certs/cert.key;
ssl_session_cache shared:SSL:5m;
ssl_session_timeout 10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
#ssl_dhparam /etc/nginx/certs/dhparams.pem;
# use the line above if you generated a dhparams file
ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA';
ssl_buffer_size 8k;
location / {
proxy_pass http://website;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto http;
proxy_redirect http:// $scheme://;
}
}
# redirect http to https here
server {
listen 80;
listen [::]:80;
server_name yourdomain.here.com;
return 301 https://$server_name/;
}
答案2
尝试这个:
upstream app_server {
server 127.0.0.1:8000 fail_timeout=0;
}
server {
# port to listen on. Can also be set to an IP:PORT
listen 80;
listen 443 ssl;
ssl_certificate /etc/ssl/pyhub_crt.crt;
ssl_certificate_key /etc/ssl/pyhub.key;
server_name www.pyhub.co;
server_name_in_redirect off;
access_log /opt/bitnami/nginx/logs/access.log;
error_log /opt/bitnami/nginx/logs/error.log;
location /E0130777F7D5B855A4C5DEB138808515.txt {
root /home/bitnami;
}
location / {
try_files @proxy_to_app;
}
location @proxy_to_app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
proxy_pass_header Server;
proxy_set_header Host $host;
proxy_set_header X-Scheme $scheme;
proxy_set_header X-SSL-Protocol $ssl_protocol;
}
}
请注意,这将(正确地)终止 SSL 于nginx
而不是gunicorn
。您还拼错了Protocal
;X-SSL-Protocal
已在我的示例中修复 :)
看这里gunicorn
有关使用 进行部署的规范信息nginx
。
答案3
我应该早就发布这个答案,因为我的网站已经运行了很长一段时间了。但下面是我如何使我的配置正常工作的。
在对 Nginx 配置进行了大量调整后,我感到非常沮丧,于是放弃了。因此,我删除了该服务器,重新安装了所有东西,并从托管它的存储库克隆了我网站的源代码。完成此操作后,我使用以下 Nginx 配置使其正常工作:
upstream djangosite {
server 127.0.0.1:8000 fail_timeout=2s;
}
server {
# port to listen on. Can also be set to an IP:PORT
listen 443 ssl;
server_name pyhub.co;
ssl_certificate /etc/ssl/pyhub.crt;
ssl_certificate_key /etc/ssl/pyhub.key;
location / {
proxy_pass http://djangosite;
}
server {
listen 80;
server_name pyhub.co;
return 301 https://$server_name$request_uri;
}
直到今天,我仍然不知道是什么导致了我的配置问题,或者为什么我必须获得另一台全新安装的服务器,但我很高兴它能正常工作。我会选择已经提供的两个优秀答案中的一个,但它们都没有给我解决方案,我不想选择我自己的,因为我认为我遇到的问题太具体了,选择这个答案可能无法在将来帮助很多人。