Nginx is the most popular HTTP server next to Apache, and it is also a reverse proxy and mail proxy server. The name nginx comes from "engine X" which sounds like a secret weapon, and in fact, it is remarkable for its high performance, low-resource usage, and legacy stability.
Install nginx
Install nginx is very easy, you can follow the instruction from the official nginx website.
Here is an example for debian 8:
1.Add apt source
vi /etc/apt/sources.list.d/nginx.list
deb http://nginx.org/packages/debian/ stretch nginx
2.Import apt key
wget --quiet -O - http://nginx.org/keys/nginx_signing.key | apt-key add -
3.Update packages and install
apt-get update
apt-get install nginx
Optimize configuration
1.Edit the main configuration
vi /etc/nginx/nginx.conf
Change worker_processes to the core number of the CPU, 2, 4 or 8.
worker_processes 2;
Add epoll for events, worker_connections is the max number of concurrency connections per process.
events {
use epoll;
worker_connections 1024;
}
Optimize TCP connections
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
}
Enable gzip compression
http {
gzip on;
#gzip_static on;
gzip_comp_level 5;
gzip_min_length 1k;
gzip_proxied any;
gzip_types text/plain text/javascript text/xml aplication/xml application/xml+rss;
gzip_vary on;
}
2.Edit the default configuration
vi /etc/nginx/conf.d/default.conf
Listening for IPv6
listen [::]:80;
Change document root and index
mkdir -p /var/www/html
location / {
root /var/www/html;
index index.html index.htm;
}
Configuration for scripts(uwsgi)
1.Configuration for PHP
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html index.htm index.php;
}
location ~ \.php$ {
root /var/www/html;
uwsgi_pass unix:/var/tmp/uwsgi_php.sock;
uwsgi_modifier1 14;
include uwsgi_params;
}
}
2.Configuration for Python
server {
listen 80;
server_name localhost;
location / {
uwsgi_pass unix:/var/tmp/uwsgi_django.sock;
uwsgi_modifier1 30;
include uwsgi_params;
}
}
3.Configuration for Redmine
server {
listen 80;
server_name localhost;
root /var/www/redmine/public;
location ~* \.(ico|css|js|html)$ {
root /var/www/redmine/public;
}
location ~* /images/ {
root /var/www/redmine/public;
}
location / {
uwsgi_pass unix:/var/tmp/uwsgi_redmine.sock;
uwsgi_modifier1 7;
include uwsgi_params;
}
}
4.Configuration for Ghost
upstream ghost {
server unix:/var/tmp/ghost.sock;
}
server {
listen 80;
server_name localhost;
location /content/images {
alias /var/www/ghost/content/images;
}
location /assets {
alias /var/www/ghost/content/themes/casper/assets;
}
location /public {
alias /var/www/ghost/core/built/public;
}
location /ghost/scripts {
alias /var/www/ghost/core/built/scripts;
}
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#Enable SSL
#proxy_set_header X-Forwarded-Proto https;
proxy_pass http://ghost;
}
access_log off;
}
5.Configuration for SSL
server {
listen 443 ssl;
server_name localhost;
ssl_certificate /etc/nginx/cert.pem;
ssl_certificate_key /etc/nginx/cert.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root /var/www/html;
index index.html index.htm;
}
}
In order to use unix socket, nginx and uwsgi must both run as the user nginx. See my previous post for uwsgi configuration.