Nginx

Nginx 重定向到 http://localhost

  • January 28, 2022

情況簡圖

  • 我無法使用域地址,server_name因為我無法控制 DNS 伺服器。我必須使用公共 IP 連接到我的 Web 伺服器。
  • 所以我設置server_name_;,但是當我請求http://firewall-public-ip:5000它重定向到http://localhost:5000.
  • 我通常可以打開其他不使用重定向的頁面。例如,我可以訪問http://firewall-public-ip:5000/login並登錄,但它會重定向到,http://localhost:5000/login因為登錄頁面在登錄後使用重定向。

nginx.conf:

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
   worker_connections 1024;
}

http {
   log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                     '$status $body_bytes_sent "$http_referer" '
                     '"$http_user_agent" "$http_x_forwarded_for"';

   access_log  /var/log/nginx/access.log  main;

   sendfile            on;
   tcp_nopush          on;
   tcp_nodelay         on;
   keepalive_timeout   65;
   types_hash_max_size 4096;
   proxy_hide_header X-Powered-By;
   proxy_hide_header Server;

   include             /etc/nginx/mime.types;
   default_type        application/octet-stream;

   # Load modular configuration files from the /etc/nginx/conf.d directory.
   # See http://nginx.org/en/docs/ngx_core_module.html#include
   # for more information.
   include /etc/nginx/conf.d/*.conf;

server {
   listen 5000;

   server_name _;
   server_name_in_redirect off;
   ssl_protocols TLSv1.2;

   location '/' {
       proxy_pass http://unix:/var/sockets/gunicorn.sock;
   }
 }
}

我該如何解決?同樣,我不能使用此伺服器的域地址。

*EDIT 添加了應用程序重定向

@blueprint.route('/')
def route_default():
   return redirect(url_for('authentication_blueprint.login'))


@blueprint.route('/login', methods=['GET', 'POST'])
def login():
   login_form = LoginForm(request.form)
   if 'login' in request.form:

       # read form data
       username = request.form['username']
       password = request.form['password']

       # Locate user
       user = Users.query.filter_by(username=username).first()

       # Check the password
       if user and verify_pass(password, user.password):

           login_user(user)
           return redirect(url_for('authentication_blueprint.route_default'))

       # Something (user or pass) is not ok
       return render_template('accounts/login.html',
                              msg='Wrong user or password',
                              form=login_form)

   if not current_user.is_authenticated:
       return render_template('accounts/login.html',
                              form=login_form)
   return redirect(url_for('home_blueprint.index'))

apps.authentication.init.py

from flask import Blueprint

blueprint = Blueprint(
   'authentication_blueprint',
   __name__,
   url_prefix=''
)

nginx 不會使用此配置發送任何重定向。

重定向來自作為proxy_pass目標的應用程序。該應用程序很可能有一個“基本 URL”設置,您需要在其中放置您的 IP 地址。

引用自:https://serverfault.com/questions/1091431