How To Setup a NGINX Virtual Host – Easy Guide

In this tutorial we are going to learn how to install and configuration of virtual hosts “Server Blocks” Nginx on your Linux server. Virtual hosts such as nginx are used for running two or more domains or websites using just one server which you can learn more about in this hosting fundamentals course. Here’s a brief tutorial that shows you how to create a virtual host or server block on Nginx web server. This guide assumes that you’ve been following along from the previous tutorial: How to Install and Configure a NGINX Server.

Setup a NGINX Virtual Host

Create a New Directory

cd /var/www
mkdir -p wpcademy.com/{public_html,logs,stats}
mkdir -p wpcademy.com/{public_html,logs,stats}
Create vhost conf file
#nano /etc/nginx/conf.d/wpcademy.com.conf

server {
   listen  80;
   server_name  wpcademy.com www.wpcademy.com;
 
   access_log  /var/www/wpcademy.com/logs/access.log ;
   error_log    /var/www/wpcademy.com/logs/error.log ;
 
   location / {
       root   /var/www/wpcademy.com/public_html;
       index  index.php index.html index.htm;
 
   }
 
   error_page   500 502 503 504  /50x.html;
   location = /50x.html {
       root   /var/www/wpcademy.com/public_html;
   }
 
  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  location ~ .php$ {
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
root    /var/www/wpcademy.com/public_html;
fastcgi_param  SCRIPT_FILENAME  /var/www/wpcademy.com/public_html$fastcgi_script_name;
include fastcgi_params;
}
 
 
   location ~ /.ht {
       deny  all;
   }
}
# nano /etc/nginx/conf.d/wpcademy.com.conf
server {
   listen  80;
   server_name  wpcademy.com www.wpcademy.com;
 
   access_log  /var/www/wpcademy.com/logs/access.log ;
   error_log    /var/www/wpcademy.com/logs/error.log ;
 
   location / {
       root   /var/www/wpcademy.com/public_html;
       index  index.php index.html index.htm;
 
   }
 
   error_page   500 502 503 504  /50x.html;
   location = /50x.html {
       root   /var/www/wpcademy.com/public_html;
   }
 
  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  location ~ .php$ {
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
root    /var/www/wpcademy.com/public_html;
fastcgi_param  SCRIPT_FILENAME  /var/www/wpcademy.com/public_html$fastcgi_script_name;
include fastcgi_params;
}
 
   location ~ /.ht {
       deny  all;
   }
}

Add vhost on nginx.conf

# nano /etc/nginx/nginx.conf
### add line like this on http section:
include /etc/nginx/conf.d/*.conf;

Restarting nginx and php-fpm

# /etc/init.d/nginx restart
# /etc/init.d php-fpm restart

Note: Please make sure that all the domain names are propagated and are properly directed to your servers ip address, if not you will not able able to check if your new configuration works or not.

Congratulation’s! You have successfully installed server blocks Nginx. Thanks for using this tutorial for installing Virtual hosts Nginx on Linux system. For additional help or useful information, we recommend you to check the official Nginx web site

How To Fix 504 Gateway Time-out on Nginx Web Server

Fix 504 Gateway Time-out on Nginx

In this tutorial we are going to learn how to fix 504 gateway time-out on Nginx web server on Linux server. If you run a Nginx web server you may have already encountered the annoying 504 Gateway Time-out errors. This is pretty common error, are generated most probably by the PHP max execution time limit or by the FastCGI read timeout settings.

This article assumes you have at least basic knowledge of linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo’ to the commands to get root privileges. I will show you through the step by step installation to fix nginx 504 gateway timeout on the nginx webserver.

Fix 504 Gateway Time-out on Nginx

Changes in php.ini

Try raising max_execution_time setting in php.ini file (CentOS path is /etc/php.ini):

 max_execution_time = 150

Changes in PHP-FPM

Try raising request_terminate_timeout setting in php.ini file (CentOS path is /etc/php-fpm.d):

 request_terminate_timeout = 150

Changes in Nginx Config

Finally, add fastcgi_read_timeout variable inside our Nginx virtual host configuration:

location ~* \.php$ {
    include         fastcgi_params;
    fastcgi_index   index.php;
    fastcgi_read_timeout 150;
    fastcgi_pass    127.0.0.1:9000;
    fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
}

Reload PHP-FPM and Nginx

 
service php-fpm restart
service nginx restart

For Nginx as Proxy for Apache web server, this is what you have to try to fix the 504 Gateway Timeout error:

Add following variables to nginx.conf file:

proxy_connect_timeout       600;
proxy_send_timeout          600;
proxy_read_timeout          600;
send_timeout                600;

Once complete, simply reload Nginx:

 service nginx restart

Congratulation’s! You have successfully fix error nginx 504 gateway time out. Thanks for using this tutorial for fix 504 gateway timeout error in Linux system. For additional help or useful information, we recommend you to check the official Nginx web site.

How To Enable Gzip Compression on Nginx CentOS

Enable Gzip Compression on Nginx CentOS

Nginx is one of the most popular web servers in the world and is responsible for hosting some of the largest and highest-traffic sites on the internet. It is more resource-friendly than Apache in most cases and can be used as a web server or a reverse proxy. So today we’re going to learn how to setup enable Gzip compression on Nginx on CentOS 6 or 7. Compressing your scripts and images is a good idea to optimize your website’s load times.

This article assumes you have at least basic knowledge of linux, know how to use the shell, and most importantly, you host your site on your own VPS. In this post, I will talk about an easy way to enable GZIP compression on nginx servers. It’s really not that difficult. Let’s start with Nginx.

Enable Gzip Compression on Nginx

Step 1. Configure nginx.conf (/etc/nginx/nginx.conf)

gzip on;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_proxied any;
gzip_min_length  1100;
gzip_buffers 16 8k;
gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_disable "MSIE [1-6].(?!.*SV1)";
gzip_vary on;

Step 2. Now, you simply need to restart your server.

 service nginx restart

If you wish to test if GZIP is enabled, use this command:

 curl -H "Accept-Encoding: gzip" -I https://wpcademy.com

With that file now in place, restart your server and you will now be serving site assets with gzip compression. Google takes site speed into account when ranking and placing your sites in their search engine so do your users a favor and strive for the fastest site possible, especially for mobile users.

Congratulation’s! You have successfully enable Gzip on Nginx. Thanks for using this tutorial for enable gzip compression Nginx on Linux system. For additional help or useful information, we recommend you to check the official Nginx web site.