django:nginx:HTTP_HOST不显示端口号



我有以下代码在生产中运行

Nginx配置如下:

# first we declare our upstream server, which is our Gunicorn application
upstream hello_server {
# docker will automatically resolve this to the correct address
# because we use the same name as the service: "djangoapp"
server webapp:8888;
}
# now we declare our main server
server {
listen 8558;
server_name localhost;
location / {
# everything is passed to Gunicorn
proxy_pass http://hello_server;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
}

Nginx服务器有端口转发:8555:8558

运行的gunicorn命令是

gunicorn --bind :8888 basic_django.wsgi:application

现在在我的浏览器中,我打开这个网址:

http://127.0.0.1:8555/login_register_password/user_login_via_otp_form_email

现在我的一个视图中的代码是

prev_url = request.META['HTTP_REFERER']
# EG: prev_url = http://127.0.0.1:8555/login_register_password/user_login_via_otp_form_email
# we want to get the url from namespace . We use reverse. But this give relative url not the full url with domain
login_form_email_url_reverse = reverse("login_register_password_namespace:user_login_via_otp_form_email")
# EG: login_form_email_url_reverse = "/login_register_password/user_login_via_otp_form_email"
# to get the full url we have to use do the below
login_form_email_url_reverse_full = request.build_absolute_uri(login_form_email_url_reverse)
# EG: login_form_email_url_reverse_full = "http://127.0.0.1/login_register_password/user_login_via_otp_form_email"

我正在执行prev_urllogin_form_email_url_reverse_full相同,但不同

prev_url域是http://127.0.0.1:8555,而login_form_email_url_reverse_full域是http://127.0.0.1

为什么会发生这种情况。

在开发服务器中不会发生这种情况。使用runserver

"HTTP_HOST": "127.0.0.1:8555",
"HTTP_REFERER": "http://127.0.0.1:8555/login_register_password/user_login_via_otp_form_email",

与nginx服务器一样:HTTP_HOST发生变化,即现在没有端口号

"HTTP_HOST": "127.0.0.1",
"HTTP_REFERER": "http://127.0.0.1:8555/login_register_password/user_login_via_otp_form_email",

我通过更改解决了问题

proxy_set_header Host $host;

proxy_set_header Host $http_host;

nginxlocal.confserver {}

答案来自https://serverfault.com/a/916736/565479

最新更新