Score:0

Nginx redirects to http://localhost

cz flag

Brief image of the situation

  • I can't use domain address for server_name because I can't control DNS server. I must use public IP to connect to my web server.
  • So I set server_name to _;, but when I request http://firewall-public-ip:5000 it redirects to http://localhost:5000.
  • I can normally open other pages that doesn't use redirection. For example, I can access http://firewall-public-ip:5000/login and login, but then it redirects to http://localhost:5000/login because the login page use redirection after logging in.

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;
    }
  }
}

How do I fix this? Again, I cannot use domain address to this server.

*EDIT added the application redirect

@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=''
)
Score:1
us flag

nginx does not send any redirect with this configuration.

The redirect is coming from your application that is the proxy_pass target. The application most likely has a "base URL" setting, where you need to put your IP address.

Lunartist avatar
cz flag
The reason I was questioning nginx.conf was because of this : https://serverfault.com/questions/186896/nginx-redirecting-to-localhost
Lunartist avatar
cz flag
Edited the question to include application redirect. Do you think I should reopen the question with Flask tag?
Lunartist avatar
cz flag
FYI: The server opens normally if I don't use Nginx and just use `gunicorn --bind 0.0.0.0:5000 --user bpimanager run:app`.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.