Multi site configuration scheme for Nginx

  • 2020-05-13 04:34:58
  • OfStack

When we have an VPS host, we don't want to waste VPS's powerful resources (compared to sharing a host with more than 1000 sites on one machine), so we often have the idea that VPS can do something. It's a good idea to host multiple websites or blogs, but how do you configure the web server to host multiple websites/blogs on one VPS? How do I access multiple sites/domains via 1 IP? This is the virtual hosting functionality that most web servers support. Here we describe how to configure virtual hosting with nginx step by step.

nginx is a small and efficient web server developed by the Russian programmer Igor Sysoev. Although nginx is small in size, it can also support virtual hosting like other web servers. That is, one IP corresponds to multiple domain names to support multi-site access, just like one IP corresponds to one site 1, so it is "virtual". You can put as many sites as you want under one IP, as long as the hard drive is big enough.

Here, the configuration of 2 sites (2 domain names) is taken as an example. The n sites can be adjusted accordingly, assuming:


IP address : 202.55.1.100
 The domain name 1 example1.com  On the  /www/example1
 The domain name 2 example2.com  On the  /www/example2

The basic idea and steps to configure nginx virtual hosting are as follows:

Put the two sites example1.com, example2.com into a directory accessible to nginx /www/
Create one nginx profile for each site, example1.com.conf, example2.com.conf, and put the profile into /etc/nginx/vhosts/
Then add a sentence include to /etc/ nginx.conf to include all the configuration files created in step 2 (with *)

Restart nginx

The specific process

The following is the specific configuration process:

1. Create the vhosts directory under /etc/nginx

mkdir /etc/nginx/vhosts

2. Create a file named example1.com.conf in /etc/nginx/vhosts/ and copy the following


server { 
listen 80; 
server_name example1.com www. example1.com; 
access_log /www/access_ example1.log main; 
location / { 
  root /www/example1.com; index index.php index.html index.htm; 
} 
error_page 500 502 503 504 /50x.html;
location = /50x.html {
 root /usr/share/nginx/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; 
fastcgi_param SCRIPT_FILENAME /www/example1.com/$fastcgi_script_name; 
include fastcgi_params; 
} 
location ~ /.ht { deny all; } }

3. Create a file named example2.com.conf in /etc/nginx/vhosts/ and copy the following


server { 
listen 80;
server_name example2.com www. example2.com; 
access_log /www/access_ example1.log main; 
location / 
{ 
root /www/example2.com; index index.php index.html index.htm; 
} 
error_page 500 502 503 504 /50x.html; 
location = /50x.html {
 root /usr/share/nginx/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; 
fastcgi_param SCRIPT_FILENAME /www/example2.com/$fastcgi_script_name; 
include fastcgi_params; 
} 
location ~ /.ht { deny all; } }

4. Open /etc/ nginix.conf file and add include to the corresponding location to include the above two files

user nginx; worker_processes 1; # main server error log error_log/var/log/nginx/error log; pid/var/run/nginx. pid; events {worker_connections 1024; } # main server config http {include mime.types; default_type application/octet - stream; log_format '$remote_addr - $remote_user [$time_local] $request '"$body_status "$body_referer "" "$http_user_agent" "$http_x_forwarded_for" '; sendfile on; # tcp_nopush on; # keepalive_timeout 0; keepalive_timeout 65; gzip on; server {listen 80; server_name _; access_log/var/log/nginx/access log main; server_name_in_redirect off; location / {root usr/share/nginx/html; index index. html; }} # include all of the virtual host configuration file include usr/local etc/nginx/vhosts / *; }

5. Restart Nginx

/etc/init.d/nginx restart


Related articles: