How to configure multiple sites and bind multiple domain names in nginx server

user:visitors Date:2022-02-25 16:47:511829

When using a common Linux web server to build a website running environment, generally only one site is set by default, so how to realize the binding of multiple sites?

First we open the nginx.conf configuration file in the usr/lcoal-nginx/conf/ directory.

Add the following code:

include /usr/local/nginx/conf/vhosts/*.conf

That is, add the configuration file of the independent website that needs to be created in the configuration module of nginx.

Create a new vhosts folder in the conf folder of the nginx folder.

Create a configuration file vhost.conf:

Add the code as follows:

server {

listen 80;

server_name www.xxx.com xxx.com;

location / {

root /data/wwwroot/wz1;

try_files $uri $uri/ /index.html;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

The listening port set by listen is 80, server_name sets the domain name bound to the host and separated by multiple available spaces, and then the root in location sets the directory wz1 of the website, and continues to set the page returned by the server error to simply set the binding of one of the websites. When the user accesses the website domain name, the request will reach the nginx80 port, and then the adapter will return the static files in the bound server directory.

In the same way, we can create multiple websites in the vhosts directory, such as: vhosta.conf, vhostb.conf, vhostc.conf

Modify the domain name bound to server_name and the website directory specified by root /data/wwwroot/ to complete the binding of multi-site and multi-domain names.

Of course we can also set it in a file:

Copy the server code segment in vhost.conf into multiple copies, and then modify the bound domain name and directory.

server {

listen 80;

server_name www.xxx.com xxx.com;

location / {

root /data/wwwroot/wz1;

try_files $uri $uri/ /index.html;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

server {

listen 80;

server_name www.bbb.com bbb.com;

location / {

root /data/wwwroot/wz2;

try_files $uri $uri/ /index.html;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

If the website also needs to be accessible with https, we also need to configure https for each site.

Modify the code to:

server {

listen 80;

server_name www.xxx.com xxx.com;

return 301 https://www.xxx.com$request_uri;

location / {

root /data/wwwroot/wz1;

try_files $uri $uri/ /index.html;

}

}

server {

listen 443 ssl;

server_name www.xxx.com xxx.com;

ssl_certificate /usr/local/nginx/cert/wz1/public key.pem;

ssl_certificate_key /usr/local/nginx/cert/wz1/private key.key;

location / {

root /data/wwwroot/wz1;

try_files $uri $uri/ /index.html;

}

}

Compared with the configuration of http, we newly added return 301 https://www.xxx.com$request_uri; in the code, its function is to make http automatically jump to https. If you don't need it, you can delete this line of code. Add Configured port 443 and set the ssl certificate ssl_certificate public key and ssl_certificate_key private key for https encryption.

For multiple sites, create multiple copies in vhosts or copy and modify the code in the vhost file.

Finally, save the settings and restart the nginx server to complete the configuration of multiple sites for the nginx server under Linux.

Popular articles
latest article