Skip to content

Latest commit

 

History

History

nginx

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

NGINX

Installation

sudo apt-get update
sudo apt-get install nginx

Check status

systemctl status nginx

top - displays linux processes (incl user, PID, command, cpu mem usage)

Allow nginx on firewall

sudo ufw status
sudo ufw app list
sudo ufw allow 'Nginx HTTP'

Nginx Configuration

/etc/nginx/nginx.conf

To see configuration parameters run nginx -V

Nginx runs under www-data user by default.

Apps Configuration - Simple

For quick setup or simple apps you might directly extend /etc/nginx/nginx.conf.

In the below example we are fowarding http requests to the running docker container and gunicorn web server that both sits on the same server as Nginx:

...

http {

...

  server {
    listen 80;
    server_name my.gunicorn.app.com;
    location / {
      proxy_pass http://127.0.0.1:8001;
      proxy_set_header Host $http_host;
     }
  }

  server {
    listen 80;
    server_name my.docker.app.com;
    location / {
      proxy_pass http://127.0.0.1:8002;
      proxy_set_header Host $http_host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     }
  }
 
}

Redirect to different location and protocol (HTTP 301)

server {
        listen 80;
        server_name my-domain.com;
        location / {
            return 301 https://secured-domain.com$request_uri;
        }
}

Apps Configuration - Robust

Each Nginx virtual server should be described by a file in the /etc/nginx/sites-available directory. You select which sites you want to enable by making symbolic links to those in the /etc/nginx/sites-enabled directory.

Create new file in /etc/nginx/sites-available/myapp.nginx.conf Example configuration: https://gist.github.com/postrational/5747293#file-hello-nginxconf Important is to define hostname, port and proxy_pass either to gunicorn directly via IP and port or using linux socket file that gunicorn uses.

To enable site create symlink in sites-enabled:

ln -s /etc/nginx/sites-available/myapp.nginx.conf /etc/nginx/sites-enabled/myapp.nginx.conf

Restart and Reload

service nginx reload - checks configuration first before restarting (safer).

service nginx restart - stops and starts nginx

Further reading