Nginx 不转发原因短语
Nginx not forwarding reason phrase
我正在使用 Gunicorn 托管我的 Django 应用程序。发生的事情如下
Django 的示例响应如下
@require_http_methods(['POST'])
def register(request):
body = json.loads(request.body.decode('utf-8'))
try:
email = body['email']
except:
return HttpResponse(status=400, reason="Email must be provided.")
在开发过程中,Django 应用程序 运行 使用命令 python manage.py runserver
并且完全没有问题。它总是以
响应
400 Email must be provided
但是在生产环境中,托管如下
Procfile
web: gunicorn app.wsgi
release: python manage.py migrate
wsgi.py
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings')
application = get_wsgi_application()
然而,当发出请求时,响应如下。
400
没有正确转发原因我错过了什么?
*编辑 1
生产和开发之间也有一个不同之处,那就是在生产环境中,gunicorn 生活在 nginx 后面,而开发则没有。请问这个问题是Nginx没有转发local reason phase导致的吗,如果是,是否可以更改?
*编辑 2
下面是我的nginx.conf
server {
listen [::]:80;
listen 80;
server_name api.app.com;
access_log /var/log/nginx/app-access.log;
error_log /var/log/nginx/app-error.log;
return 301 https://$host:443$request_uri;
}
server {
listen [::]:443 ssl http2;
listen 443 ssl http2;
server_name api.app.com;
access_log /var/log/nginx/app-access.log;
error_log /var/log/nginx/app-error.log;
ssl_certificate /home/dokku/app/tls/server.crt;
ssl_certificate_key /home/dokku/app/tls/server.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers off;
keepalive_timeout 70;
location / {
gzip on;
gzip_min_length 1100;
gzip_buffers 4 32k;
gzip_types text/css text/javascript text/xml text/plain text/x-component application/javascript application/x-javascript application/json application/xml application/rss+xml font/truetype application/x-font-ttf font/opentype application/vnd.ms-fontobject image/svg+xml;
gzip_vary on;
gzip_comp_level 6;
proxy_pass http://app-5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header X-Request-Start $msec;
}
include /home/dokku/app/nginx.conf.d/*.conf;
error_page 400 401 402 403 405 406 407 408 409 410 411 412 413 414 415 416 417 418 420 422 423 424 426 428 429 431 444 449 450 451 /400-error.html;
location /400-error.html {
root /var/lib/dokku/data/nginx-vhosts/dokku-errors;
internal;
}
error_page 404 /404-error.html;
location /404-error.html {
root /var/lib/dokku/data/nginx-vhosts/dokku-errors;
internal;
}
error_page 500 501 503 504 505 506 507 508 509 510 511 /500-error.html;
location /500-error.html {
root /var/lib/dokku/data/nginx-vhosts/dokku-errors;
internal;
}
error_page 502 /502-error.html;
location /502-error.html {
root /var/lib/dokku/data/nginx-vhosts/dokku-errors;
internal;
}
}
upstream app-5000 {
server 172.17.0.8:5000;
}
reason短语没有被转发的原因是nginx监听http2。 Http2去掉了http1.1的reason短语,解决的办法就是强制nginx监听http1.1。
nginx.conf
...
server {
listen [::]:443 ssl;
listen 443 ssl;
server_name api.app.com;
access_log /var/log/nginx/app-access.log;
error_log /var/log/nginx/app-error.log;
...
这样就解决了reason短语没有被转发的问题
我正在使用 Gunicorn 托管我的 Django 应用程序。发生的事情如下
Django 的示例响应如下
@require_http_methods(['POST'])
def register(request):
body = json.loads(request.body.decode('utf-8'))
try:
email = body['email']
except:
return HttpResponse(status=400, reason="Email must be provided.")
在开发过程中,Django 应用程序 运行 使用命令 python manage.py runserver
并且完全没有问题。它总是以
400 Email must be provided
但是在生产环境中,托管如下
Procfile
web: gunicorn app.wsgi
release: python manage.py migrate
wsgi.py
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings')
application = get_wsgi_application()
然而,当发出请求时,响应如下。
400
没有正确转发原因我错过了什么?
*编辑 1
生产和开发之间也有一个不同之处,那就是在生产环境中,gunicorn 生活在 nginx 后面,而开发则没有。请问这个问题是Nginx没有转发local reason phase导致的吗,如果是,是否可以更改?
*编辑 2
下面是我的nginx.conf
server {
listen [::]:80;
listen 80;
server_name api.app.com;
access_log /var/log/nginx/app-access.log;
error_log /var/log/nginx/app-error.log;
return 301 https://$host:443$request_uri;
}
server {
listen [::]:443 ssl http2;
listen 443 ssl http2;
server_name api.app.com;
access_log /var/log/nginx/app-access.log;
error_log /var/log/nginx/app-error.log;
ssl_certificate /home/dokku/app/tls/server.crt;
ssl_certificate_key /home/dokku/app/tls/server.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers off;
keepalive_timeout 70;
location / {
gzip on;
gzip_min_length 1100;
gzip_buffers 4 32k;
gzip_types text/css text/javascript text/xml text/plain text/x-component application/javascript application/x-javascript application/json application/xml application/rss+xml font/truetype application/x-font-ttf font/opentype application/vnd.ms-fontobject image/svg+xml;
gzip_vary on;
gzip_comp_level 6;
proxy_pass http://app-5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header X-Request-Start $msec;
}
include /home/dokku/app/nginx.conf.d/*.conf;
error_page 400 401 402 403 405 406 407 408 409 410 411 412 413 414 415 416 417 418 420 422 423 424 426 428 429 431 444 449 450 451 /400-error.html;
location /400-error.html {
root /var/lib/dokku/data/nginx-vhosts/dokku-errors;
internal;
}
error_page 404 /404-error.html;
location /404-error.html {
root /var/lib/dokku/data/nginx-vhosts/dokku-errors;
internal;
}
error_page 500 501 503 504 505 506 507 508 509 510 511 /500-error.html;
location /500-error.html {
root /var/lib/dokku/data/nginx-vhosts/dokku-errors;
internal;
}
error_page 502 /502-error.html;
location /502-error.html {
root /var/lib/dokku/data/nginx-vhosts/dokku-errors;
internal;
}
}
upstream app-5000 {
server 172.17.0.8:5000;
}
reason短语没有被转发的原因是nginx监听http2。 Http2去掉了http1.1的reason短语,解决的办法就是强制nginx监听http1.1。
nginx.conf
...
server {
listen [::]:443 ssl;
listen 443 ssl;
server_name api.app.com;
access_log /var/log/nginx/app-access.log;
error_log /var/log/nginx/app-error.log;
...
这样就解决了reason短语没有被转发的问题