nginx configuration virtual host vhost method details

  • 2020-05-12 07:04:36
  • OfStack

preface

The so-called virtual host, is that through several different url address, can reach the nginx environment, but for different url, the processing logic is different.
nginx support virtual host, but the browser and other clients do not know, so the virtual host several addresses, should be pointing to nginx ip address, virtual host function is normal.

The environment

System environment: CentOS 6.7

nginx version: nginx/1.8.1

plan

For example, the configuration file is located at: /opt/nginx/conf/nginx.conf

At the end of http {}, add the following line:


include vhosts/*.conf;

Or main configuration file contains/opt/nginx/conf/vhosts/directory of all *. conf configuration files. [note: vhosts directory needs to be created manually]

After you add the child configuration file, you need to pass it /opt/nginx/sbin/nginx -t Check the configuration file for correctness

The sample

Here is vhost for one php site:


vim www.domain.com.conf
server {
 listen 80;
 server_name www.domain.com;
 
 location ~ \.php$ {
 root  /opt/www;
 fastcgi_pass 127.0.0.1:9000;
 fastcgi_index index.php;
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 include fastcgi_params;
 }
 location / {
 root  /opt/www;
 index  index.php;
 }
}

Advanced features

Virtual host based on domain name

Nginx first selects which virtual host will handle the request. For example, the following three virtual hosts are listening on port * : 80


server {
 listen 80;
 server_name example.org www.example.org;
 ...
}

server {
 listen 80;
 server_name example.net www.example.net;
 ...
}

server {
 listen 80;
 server_name example.com www.example.com;
 ...
}

In this configuration, nginx simply checks the "Host" header of the request to determine which virtual host should handle the request. If the Host header does not match any one virtual host, or if the request does not contain an Host header at all, nginx will dispatch the request to the default virtual host defined on this port. In the above configuration, the first virtual host listed is the default virtual host for nginx -- this is the default behavior for nginx. Furthermore, you can explicitly set a host as the default virtual host, that is, set the "default_server" parameter in the "listen" directive:


server {
 listen 80 default_server;
 server_name example.net www.example.net;
 ...
}

Virtual host based on a mix of domain name and IP

In the following configuration, there are several virtual hosts listening at different addresses:


server {
 listen 192.168.1.1:80;
 server_name example.org www.example.org;
 ...
}

server {
 listen 192.168.1.1:80;
 server_name example.net www.example.net;
 ...
}

server {
 listen 192.168.1.2:80;
 server_name example.com www.example.com;
 ...
}

In this configuration, nginx first tests whether the requested IP address and port match the listen directive configuration in an server configuration block. Then nginx continues to test whether the requested Host header matches a value of server_name in the server block. If the hostname is not found, nginx will pass the request to the default virtual host for processing. For example, a request for access to www.example.com received from port 192.168.1.1:80 will be processed by the default virtual host on port 192.168.1.1:80, which in this case is the first server, because there is no virtual host named www.example.com defined on this port.

The default server is the property of the listening port, so different listening ports can be set to different default servers:


server {
 listen 192.168.1.1:80;
 server_name example.org www.example.org;
 ...
}

vim www.domain.com.conf
server {
 listen 80;
 server_name www.domain.com;
 
 location ~ \.php$ {
 root  /opt/www;
 fastcgi_pass 127.0.0.1:9000;
 fastcgi_index index.php;
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 include fastcgi_params;
 }
 location / {
 root  /opt/www;
 index  index.php;
 }
}
0

vim www.domain.com.conf
server {
 listen 80;
 server_name www.domain.com;
 
 location ~ \.php$ {
 root  /opt/www;
 fastcgi_pass 127.0.0.1:9000;
 fastcgi_index index.php;
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 include fastcgi_params;
 }
 location / {
 root  /opt/www;
 index  index.php;
 }
}
1

server_name matching order

1. The exact server_name match, for example:


vim www.domain.com.conf
server {
 listen 80;
 server_name www.domain.com;
 
 location ~ \.php$ {
 root  /opt/www;
 fastcgi_pass 127.0.0.1:9000;
 fastcgi_index index.php;
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 include fastcgi_params;
 }
 location / {
 root  /opt/www;
 index  index.php;
 }
}
2

2. String starting with * wildcard:


server {
 listen 80;
 server_name *.domain.com;
 ...
}

3. String ending with * wildcard:


vim www.domain.com.conf
server {
 listen 80;
 server_name www.domain.com;
 
 location ~ \.php$ {
 root  /opt/www;
 fastcgi_pass 127.0.0.1:9000;
 fastcgi_index index.php;
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 include fastcgi_params;
 }
 location / {
 root  /opt/www;
 index  index.php;
 }
}
4

4. Match regular expressions:


vim www.domain.com.conf
server {
 listen 80;
 server_name www.domain.com;
 
 location ~ \.php$ {
 root  /opt/www;
 fastcgi_pass 127.0.0.1:9000;
 fastcgi_index index.php;
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 include fastcgi_params;
 }
 location / {
 root  /opt/www;
 index  index.php;
 }
}
5

conclusion

The above is about nginx virtual host vhost configuration of all the content, I hope the content of this article to everyone's study or work can bring 1 definite help, if you have questions you can leave a message to communicate.


Related articles: