Multi-domain configuration for one nginx Server
The powerful regular expression support of Nginx can make the configuration of server_name flexible. If you want to create a multi-user blog, it is easy for each user to have their own second-level domain name.
Let me talk about the use of server_name:
Matching order of server_name
The server_name command in Nginx is mainly used to configure the name-based virtual host. The server_name command matches the following order after receiving the request:
1. Accurate server_name matching, for example:
server {listen 80;server_name ssdr.info www.ssdr.info;...}
2. String starting with * wildcard:
server {listen 80;server_name *.ssdr.info;...}
3. string ended with a * wildcard:
server {listen 80;server_name www.*;...}
4. Regular Expression matching:
server {listen 80;server_name ~^(?.+)\.howtocn\.org$;...}
Nginx will match the server name in the order of 1, 2, 3, 4. The search will be stopped only after one match, therefore, when using this command, we must clearly identify the matching sequence (similar to the location command ).
A very practical function of the server_name command is to use the regular expression capture function, which can streamline the configuration file as much as possible. After all, it is inconvenient to maintain the configuration file too long. The following are two specific applications:
Configure multiple sites in one server block:
server{listen 80;server_name ~^(www\.)?(.+)$;index index.php index.html;root /data/wwwsite/$2;}
The main directory of the site should be similar to the following structure:
/Data/wwwsite/ssdr.info/data/wwwsite/linuxtone.org
/Data/wwwsite/baidu.com/data/wwwsite/google.com
In this way, you can use only one server block to complete the configuration of multiple sites.
Configure multiple second-level domain names for one site in one server block.
In the actual website directory structure, we usually create a separate directory for the second-level domain name of the site. We can also use regular capture to implement multiple second-level domain names in a server block:
server{listen 80;server_name ~^(.+)?\.howtocn\.org$;index index.html;if ($host = ssdr.info){rewrite ^ http://www.ssdr.info permanent;}root /data/wwwsite/ssdr.info/$1/;}
The directory structure of the site should be as follows:
/Data/wwwsite/ssdr.info/www//data/wwwsite/ssdr.info/nginx/
In this way, when accessing www.ssdr.info, the root directory is/data/wwwsite/logs.
The if statement is used to redirect the orientation of ssdr.info to www.ssdr.info. This not only solves the access to the Home Directory of the website, but also increases the domain name weight of www.ssdr.info in seo.
Multiple Regular Expressions
If you use a regular expression in server_name and the location field below uses a regular expression match, you cannot use a reference like $1 or $2, the solution is to assign a value to a named variable through the set command:
server{listen 80;server_name ~^(.+)?\.howtocn\.org$;set $www_root $1;root /data/wwwsite/ssdr.info/$www_root/;location ~ .*\.php?$ {fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME /data/wwwsite/ssdr.info/$fastcgi_script_name;include fastcgi_params;}}
Different Nginx domain names reverse proxy to another server proxy_pass and $ host
If you want a VPS to serve as the frontend of another VPS, you must add a domain name to the front-end VPS for reverse proxy, if you add a backend VPS Domain Name one by one as the frontend VPS, this is especially troublesome. Can you enable it to automatically reverse proxy the backend VPS? You can easily implement it using proxy_pass and $ host.
The following example uses lnmp as the installation environment for ease of use.
Modify the nginx. conf file of the front-end VPS to the following:
server {listen 80;server_name $host;location / {proxy_pass http://www.31.gd/;proxy_set_header Host $host;proxy_redirect off;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_connect_timeout 60;proxy_read_timeout 600;proxy_send_timeout 600;}
Modify the following.
location /.(php|php5)?${fastcgi_pass unix:/tmp/php-cgi.sock;fastcgi_index index.php;include fcgi.conf;}location /status {stub_status on;access_log off;}location /.(gif|jpg|jpeg|png|bmp|swf)${expires 30d;}location /.(js|css)?${expires 12h;}
In this way, the frontend VPS can reverse proxy any domain name to the backend VPS, as long as the domain name is resolved to the frontend VPS, the backend VPS for domain name binding, then you can directly access
Configuration of one nginx with multiple domain names and multiple tomcat
Multiple Domain Names, two of which must support wildcard domain name resolution:
1. www.abc.com
2. www.bcd.com
3. * .efg.com
4. * .hij.com
1, 2, 3 are tomcat, and 4 are independent tomcat. One nginx front-end is deployed by configuring multiple virtual hosts.
Go to the/etc/nginx/conf. d directory. All the configuration files of the VM are stored and configured in this directory.
Configuration supports wildcard domain names
## A virtual host using mix of IP-, name-, and port-based configuration#server {listen 81;server_name *.efg.com;location / {proxy_pass http://localhost:8080;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}}## A virtual host using mix of IP-, name-, and port-based configuration#server {listen 81;server_name *.hij.com;location / {proxy_pass http://localhost:8081;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}}
The key for wildcard domain name resolution is the red part. If there is no red part, the tomcat virtual host corresponding to the backend ports 8080 and 8081 cannot obtain the domain name information, and the backend tomcat cannot obtain the corresponding domain name information.
When backend TOMCAT supports wildcard domain name resolution, you must set the host name to localhost to support wildcard Domain name Pointing.
Nginx multi-domain configuration
When you bind multiple domain names to nginx, you can write multiple domain name rules into one configuration file. You can also create multiple domain name configuration files for ease of management, each domain name creates a file, and some similar domain names can also be written in a general configuration file.
1. One file for each domain name
First, open the directory where the nginx domain name configuration file is stored:/usr/local/nginx/conf/servers. to bind the domain name www.web126.com, create a file: www.web126.com. conf and then write rules in this file, such:
Server {listen 80; server_name www.web126.com; # bind the domain name index index.htm index.html index. php; # default file root/home/www/web126.com; # The website root directory include location. conf; # Call other rules or remove them}
Then restart the nginx server, and the domain name is successfully bound.
Restart the Nginx server:/etc/init. d/nginx restart.
2. Writing multiple domain names in one file
The same applies to the rules for adding multiple domain names to a file. You only need to write down the previous single domain name repeatedly, for example:
Server {listen 80; server_name www.web126.com; # bind the domain name index index.htm index.html index. php; # default file root/home/www/web126.com; # The website root directory include location. conf; # Call other rules, you can also remove} server {listen 80; server_name msn.web126.com; # bind the domain name index index.htm index.html index. php; # default file root/home/www/msn.web126.com; # The website root directory include location. conf; # Call other rules or remove them}
3. Add 301 redirection for domain names without www
If you want to add a 301 jump to a domain name without www, it is the same as binding a domain name. First, bind a domain name without www. Instead of writing a website directory, you can perform a 301 jump, for example:
server{listen 80;server_name web126.com;rewrite ^/(.*) http://www.web126.com/$1 permanent;}
4. Add 404 web pages
To add 404 web pages, you can add them directly in the page, for example:
Server {listen 80; server_name www.web126.com; # bind the domain name index index.htm index.html index. php; # default file root/home/www/web126.com; # The website root directory include location. conf; # Call other rules, you can also remove error_page 404/404 .html ;}
Finally, you need to note that you may need to disable the IP address to directly access port 80 or prohibit the domain name not on this site from binding our IP address. In this case, we should
Put it on the first server as follows:
server{listen 80 default;server_name _;return 403;}
After learning the above four rules and methods, you can solve the problem of nginx multi-domain configuration independently.