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)
sudo ufw status
sudo ufw app list
sudo ufw allow 'Nginx HTTP'
/etc/nginx/nginx.conf
To see configuration parameters run nginx -V
Nginx runs under www-data
user by default.
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;
}
}
}
server {
listen 80;
server_name my-domain.com;
location / {
return 301 https://secured-domain.com$request_uri;
}
}
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
service nginx reload
- checks configuration first before restarting (safer).
service nginx restart
- stops and starts nginx