unknown
unknown

Reputation: 331

nginx: How can I remove port from url?

I have a vps server and domain

Nginx

/etc/nginx/sites-available/myproject and /etc/nginx/sites-enabled/myproject

server {
  listen 80;
  server_name example.com;
  location = /favicon.ico { access_log off; log_not_found off; }
  location /static/ {
    root /home/myproject;
  }
  location / {
    include proxy_params;
    proxy_pass http://unix:/run/gunicorn.sock;
  }
}

/etc/nginx/nginx.conf

...
http {
    ...
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*.*;
}

nginx -t - successful

systemctl status nginx - active

ufw status

Status: active

To                         Action      From
--                         ------      ----
8000                       ALLOW       Anywhere                  
80                         ALLOW       Anywhere                  
443                        ALLOW       Anywhere                  
8000 (v6)                  ALLOW       Anywhere (v6)             
80 (v6)                    ALLOW       Anywhere (v6)             
443 (v6)                   ALLOW       Anywhere (v6)

When I open ip of vps-server in browser I get home page of nginx.


gunicorn

/etc/systemd/system/gunicorn.service

[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=root
Group=root
WorkingDirectory=/home/myproject
ExecStart=/home/myproject/myprojectenv/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
myproject.wsgi:application
[Install]
WantedBy=multi-user.target

/etc/nginx/sites-enabled# cat /etc/systemd/system/gunicorn.socket

[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/run/gunicorn.sock
[Install]
WantedBy=sockets.target

systemctl status gunicorn - active

gunicorn --bind 0.0.0.0:8000 myproject.wsgi - working

/run/gunicorn.sock - exists


Django

/home/myproject/myproject/.settings.py

ALLOWED_HOSTS = ['example.com', 'localhost']

DATABASES = {
     'default': {
         'ENGINE': 'django.db.backends.postgresql_psycopg2',
         'NAME': 'test_db',
         'USER': 'test_user',
         'PASSWORD': 'test',
         'HOST': 'localhost',
         'PORT': '',
     }
}

I'm run a manage.py runserver example.com:8000 and get response from Django, that the server is started on example.com:8000.

I'm open web-site by url http://example.com:8000

How can I open web site by url http://example.com (without port)

Upvotes: 1

Views: 1878

Answers (1)

Ksandr Ki
Ksandr Ki

Reputation: 31

you need create named location like

upstream serv {
    server unix:/app/tmp/unicorn.socket fail_timeout=0;
}
server {
    listen 80 ;
    root /usr/html ;
    try_files $uri/index.html $app ;

location @app {
    proxy_pass http://serv ;
}
}

Upvotes: 1

Related Questions