Nginx virtual host configuration details

What is a virtual host

Virtual host is using a special software and hardware technology, he has made a run on the Internet server host is divided into running a “virtual” host, each virtual host can be an independent site, can have an independent domain name, complete with server capabilities, silly girl same host between virtual host is completely independent.

Nginx basic virtual host configuration is as follows:

Ip-based virtual hosting

In Linux and other common operating systems are allowed to add IP alias, you can add binding multiple IP addresses in a network card, so you can run multiple VIRTUAL hosts based on IP in the server of a single network card.Copy the code

Then configure three IP addresses 192.168.8.43, 192.168.8.44, and 192.168.8.45 in the nginx configuration file to support static HTML virtual hosts.

http {
       192.168.8.43 virtual host
       server {
       Listen for IP addresses and portsListen 192.168.8.43:80;# host nameServer_name 192.168.8.43;# Access the log file path
       access_log logs/server1.access.log combined;
       
       location / {
           If you can't find index.html, look for index.htm as the home page file
           index index.html index.htm;
           # HTMl Directory for storing web filesroot /data1/server1; }}192.168.8.44 virtual host
       server {
       Listen for IP addresses and portsListen 192.168.8.44:80;# host nameServer_name 192.168.8.44;# Access the log file path
       access_log logs/server2.access.log combined;
       
       location / {
           If you can't find index.html, look for index.htm as the home page file
           index index.html index.htm;
           # HTMl Directory for storing web filesroot /data1/server2; }}192.168.8.45 virtual host
       server {
       Listen for IP addresses and portsListen 192.168.8.45:80;# host nameServer_name 192.168.8.45;# Access the log file path
       access_log logs/server3.access.log combined;
       
       location / {
           If you can't find index.html, look for index.htm as the home page file
           index index.html index.htm;
           # HTMl Directory for storing web filesroot /data1/server3; }}}Copy the code

If you want to configure multiple virtual hosts, you can configure multiple server{} segments, but the listening IP address and port must be different. You can also use listen 80 to represent the listening server IP address 80 port. Distinguish different virtual hosts by server — name

Port-based virtual host

Port based virtual host is basically not a lot of use, the general situation as a simple understanding

 http {
        Configure port 1 virtual host
        server {
        Listen for IP addresses and ports
        listen  80;
        # host name
        server_name localhost;
        # Access the log file path
        access_log logs/nginx80.access.log combined;
        
        location / {
            If you can't find index.html, look for index.htm as the home page file
            index index.html index.htm;
            # HTMl Directory for storing web filesroot /data1/nginx80; }}Configure port 2 virtual host
        server {
        Listen for IP addresses and ports
        listen   81;
        # host name
        server_name localhost;
        # Access the log file path
        access_log logs/nginx81.access.log combined;
        
        location / {
            If you can't find index.html, look for index.htm as the home page file
            index index.html index.htm;
            # HTMl Directory for storing web filesroot /data1/nginx81; }}Configure port 3 virtual host
        server {
        Listen for IP addresses and ports
        listen   82;
        # host name
        server_name www.nginx82.com;
        # Access the log file path
        access_log logs/nginx82.access.log combined;
        
        location / {
            If you can't find index.html, look for index.htm as the home page file
            index index.html index.htm;
            # HTMl Directory for storing web filesroot /data1/nginx82; }}}Copy the code

Domain-based virtual hosting

Domain name based virtual host is a common kind of virtual host, just configure the local DNS server, map each host name to the correct IP address, and then configure the Nginx server, let it identify different host names can be achieved.

Domain-based approach makes many virtual hosts share an IP address, which effectively solves the problem of insufficient IP address. So if there is no special requirements, try not to use IP based configuration virtual host, try to use domain name based virtual host.

http {
       Configure domain 1 virtual host
       server {
       Listen for IP addresses and ports
       listen  80;
       # host name
       server_name www.nginx1.com;
       # Access the log file path
       access_log logs/nginx1.access.log combined;
       
       location / {
           If you can't find index.html, look for index.htm as the home page file
           index index.html index.htm;
           # HTMl Directory for storing web filesroot /data1/nginx1; }}Configure domain 2 virtual host
       server {
       Listen for IP addresses and ports
       listen   80;
       # host name
       server_name www.nginx2.com;
       # Access the log file path
       access_log logs/nginx2.access.log combined;
       
       location / {
           If you can't find index.html, look for index.htm as the home page file
           index index.html index.htm;
           # HTMl Directory for storing web filesroot /data1/nginx2; }}Configure domain 3 virtual host
       server {
       Listen for IP addresses and ports
       listen   80;
       # host name
       server_name www.nginx3.com;
       # Access the log file path
       access_log logs/nginx3.access.log combined;
       
       location / {
           If you can't find index.html, look for index.htm as the home page file
           index index.html index.htm;
           # HTMl Directory for storing web filesroot /data1/nginx3; }}}Copy the code