Nginx Virtual Host configuration is divided into three main categories:
Domain-based virtual host (z.com)
IP-based virtual host (192.168.1.42)
Port-based virtual host (z.com:2022)
Generally, when the server has more than two hosts, it is recommended that these virtual hosts be created separately and then introduced directly in the nginx.conf file (for example: include vhost/*.conf)
First, the domain-based virtual host
server {
Listen 80;
server_name z.com;
Location/{
root/home/www/zgw/;
Index index.html;
}
}
Explain:
server {} One this represents a virtual host pool
Listen 80 port (the default is 80 ports, the following is based on the domain name of the virtual host can be filled with other ports)
root/home/www/zgw/; Web page Live directory
Index index.html; Default Web page file
A. After you save, you must check that the configuration file is configured correctly:/usr/local/nginx/sbin/nginx-t (the word test is successful appears to indicate a successful configuration)
B. Go to the corresponding directory (/home/www/zgw/) to create the index.html file and write the content
C. Restart Nginx:kill-hup ' Cat/usr/local/nginx/logs/nginx.pid '
At this point you are locally accessing the domain name z.com It will go to request the extranet, will not parse your Linux configuration information
D. Must modify the local host, bind the domain name corresponding to the Linux IP (for example, IP is 192.168.1.42)
192.168.1.42 z.com
At this point, the access can appear in the index.htm just written in the content
Second, the port-based virtual host
Still take z.com domain name as an example
server {
Listen 2022; (only need to copy the domain-based virtual host server{}, change the port here to the port you want)
server_name z.com;
Location/{
root/home/www/port/;
Index index.html;
}
}
Then repeat the above action (note: z.com domain)
Access is now using z.com:2022.
Third, IP-based virtual host
server {
Listen 80;
server_name 192.168.1.42;
Location/{
root/home/www/ip/;
Index index.html;
}
}
Then repeat (a) the operation can be, here also do not need to bind host, it is worth noting that Nginx configuration file is read from the top, if you put the configuration IP of the virtual host to the Nginx original localhost server, Your access to the IP will appear in the Nginx welcome interface, rather than the information you have just configured, the order is important
Nginx: Virtual Host Configuration