Nginx Server Blocks (Virtual Hosts) Example Template

By | November 7, 2016

Advertisement from Google

nginxHere is an example of the nginx server block (Virtual Hosts) which you can use to host multiple web sites on the same server.

Just replace example.com with your own domain name. Do not forget to create all required folders and sent right permissions.


Advertisement from Google

Copy one of those templates to the /etc/nginx/sites-available/example.com

Proxy example

# Ivan Derevianko aka druss http://druss.co
# Force without domain domain without www
server {
    server_name  www.example.com;
    listen 80;
    rewrite ^(.*) http://example.com$1 permanent;
}

# Main server
server {
    server_name  example.com;
    listen 80;
    root /var/example.com/www;
    access_log /var/example.com/logs/access.log;
    error_log /var/example.com/logs/error.log;

    location / {
        proxy_pass http://localhost:5000;
	proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection keep-alive;
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

Php-fpm example

# Ivan Derevianko aka druss http://druss.co
# Force without domain domain without www
server {
    server_name  www.example.com;
    listen 80;
    rewrite ^(.*) http://example.com$1 permanent;
}

# Main server
server {
    server_name  example.com;
    listen 80;
    root /var/example.com/www;
    access_log /var/example.com/logs/access.log;
    error_log /var/example.com/logs/error.log;

    client_max_body_size 128M;

    index index.php index.html index.htm;
	
    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }
	
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *