【问题标题】:Redirect and url_for in Flask adds domain twice on prod server via Gunicorn and NginxFlask 中的 Redirect 和 url_for 通过 Gunicorn 和 Nginx 在 prod 服务器上添加域两次
【发布时间】:2018-06-04 08:13:39
【问题描述】:

我在产品服务器上的 Flask 中重定向时遇到问题。注意:这不会在本地发生,只有在使用 Nginx 和 Gunicorn 的 prod 服务器上运行时才会发生。当添加 https 和域时,问题就开始了。我不确定问题出在烧瓶还是我的 nginx 设置上。

所有重定向都会出现我的问题,但这里有一个示例:return redirect(url_for('.login'))。这里的预期输出是“domain.com/login”,但它重定向到“domain.com%2Cdomain.com/login”。当我打印 `url_for('.login') 时,它显示“/login”。

在寻找解决方案时,我发现这可能与我的 nginx 设置有关,所以它们是:

server {
    server_name domain.com www.domain.com;

    #Fix for socket.io, dont think this is related
    location /socket.io {
            proxy_pass http://127.0.0.1:8088/socket.io;
            proxy_redirect off;
            proxy_buffering off;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
}

    location / {
            include proxy_params;
            proxy_pass http://127.0.0.1:8088;
    }

listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem; # managed by 
Certbot
ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem; # managed 
by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


}
server {
if ($host = www.domain.com) {
    return 301 https://$host$request_uri;
} # managed by Certbot


if ($host = domain.com) {
    return 301 https://$host$request_uri;
} # managed by Certbot


    listen 80;
    server_name domain.com www.domain.com;
return 404; # managed by Certbot
}

请注意,我将我们的域替换为“domain.com”。

如果我可以提供更多信息,请告诉我。

编辑:解决了。我不得不在我的 nginx 配置文件中删除 include proxy_params; 行。我不知道为什么,但它奏效了。现在行为符合预期。我正在尝试将此帖子标记为已解决,但它说我需要等待几天。

【问题讨论】:

    标签: redirect nginx flask gunicorn url-for


    【解决方案1】:

    好的,我想通了。我不得不在我的 nginx 配置文件中删除 include proxy_params; 行。不知道为什么,但之后它按预期工作。

    【讨论】:

      【解决方案2】:

      我也有同样的问题。我删除了包含 proxy_params; 但它没有解决问题。如果有人可以帮助我,我将不胜感激。 这是我的 nginx 配置:

      server {
          # listen on port 80 (http)
          listen 80;
          server_name domain.com;
          location /.well-known {
              root /to/dorectory/where/the/files/are;
          }
          location / {
              # redirect any requests to the same URL but on https
              return 301 https://$host$request_uri;
          }
      }
      server {
          # listen on port 443 (https)
          listen 443 ssl;
          server_name domain.com;
      
          # location of the SSL certificate
          ssl_certificate /to/fullchain.pem;
          ssl_certificate_key /to/privkey.pem;
      
          # write access and error logs to /var/log
          access_log /to/mysite_access.log;
          error_log /to/mysite_error.log;
      
          location / {
              # forward application requests to the gunicorn server
              proxy_pass http://localhost:8000;
              proxy_set_header Host $host;
              proxy_set_header X-Real-IP $remote_addr;
              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          }
      
          location /static {
              # handle static files directly, without forwarding to the application
              alias /to/static/directory;
          }
      }
      

      【讨论】:

      猜你喜欢
      • 2019-12-27
      • 2016-03-02
      • 2017-06-06
      • 1970-01-01
      • 1970-01-01
      • 2019-09-19
      • 2017-02-14
      • 1970-01-01
      • 2014-07-02
      相关资源
      最近更新 更多