Nginx Strong regular expression support, can make server_name configuration becomes very flexible, if you want to long user blog, then each user has their own level two domain name is easy to achieve.
Let me tell you about the use of server_name:
The matching order of server_name
The server_name instruction in Nginx is mainly used to configure the name-based virtual host, and the Order of the server_name instructions after receiving the request is:
1, accurate server_name match, for example:
server {Listen, server_name Ssdr.info www.ssdr.info, ...}
2. A string starting with a * wildcard:
server {listen; server_name *.ssdr.info; ...}
3. A string ending with a * wildcard character:
server {listen; server_name www.*; ...}
4. Match Regular expression:
server {listen; server_name ~^ (?. +) \.howtocn\.org$; ... }
Nginx will match the server name in the order of 1,2,3,4, and only one match will stop the search, so when we use this command we must be clear about its matching order (similar to the location Directive).
A very useful feature of the server_name directive is the ability to use regular expression capture, which minimizes configuration files, and is inconvenient for routine maintenance of too long profiles. Here are 2 specific applications:
To configure multiple sites in one server block:
server {listen; server_name ~^ (www\.)? (.+)$; Index index.php index.html; root/data/wwwsite/$2; }
The home directory of the site should resemble this structure:
/data/wwwsite/ssdr.info/data/wwwsite/linuxtone.org/data/wwwsite/baidu.com/data/wwwsite/google.com
This allows you to use only one server block to complete the configuration of multiple sites.
Configure multiple level Two domain names for a site in one server block.
In the actual site directory structure we usually create a separate directory for the site's level two domain name, and we can use regular captures to configure multiple level two domain names in one server block:
server {listen; server_name ~^ (. +)? \.howtocn\.org$; index index.html; if ($host = ssdr.info) {Rewrite ^ http://www.ssd R.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/
This accesses www.ssdr.info when the root directory is/data/wwwsite/ssdr.info/www/,nginx.ssdr.info/data/wwwsite/ssdr.info/nginx/, and so on.
The function of the IF statement is to redirect the azimuth of the Ssdr.info to Www.ssdr.info, which solves both the website's main directory access and the Www.ssdr.info's domain weight in the SEO.
Multiple regular expressions
If you use a regular in server_name, and the following location field uses a regular match, you will not be able to use a reference such as $1,$2, and the workaround is to assign it to a named variable with a set directive:
server {listen; server_name ~^ (. +)? \.howtocn\.org$; set $www _root $; root/data/wwwsite/ssdr.info/$www _root/; locati On ~. *\.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; } }
Nginx different domain name reverse proxy to another server Proxy_pass and $host
Want to make a VPS dedicated to the front end of another VPS, back-end VPS Each add a domain name, front-end VPS will be added at the same time a domain name to reverse proxy, as the front-end VPS If one add back-end VPS domain name, then this thing particularly troublesome, can let its automatic reverse proxy backend VPS it, It can be easily implemented with Proxy_pass and $host.
For the sake of convenience, the following example sets the installation environment to LNMP
Modify the nginx.conf file of the front-end VPS to the following content:
server {listen; server_name $host; location/{Proxy_pass http://www.31.gd/; proxy_set_header host $host; proxy_redir ECT 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; }
Let's revise 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;}
This enables the front-end VPS can reverse proxy any domain name to the backend VPs, as long as the domain name to the front-end VPS, the backend VPS for domain binding, then you can directly access the
One nginx with multiple domain names multiple tomcat scenarios configuration
Multiple domain names, 2 of which need to support pan domain name resolution:
1, www.abc.com
2, www.bcd.com
3, *.efg.com
4, *.hij.com
One of the two tomcat,4 is a standalone tomcat. Front-end one nginx, by configuring multiple virtual hosts to implement the deployment.
Into the/ETC/NGINX/CONF.D directory, all the virtual host configuration files are stored in this directory, configuration.
Configuring support for Pan 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 to the pan domain name resolution is the red part, if there is no red part, the corresponding Tomcat virtual host of the backend 8080 and 8081 will not be able to obtain the domain name information, resulting in the backend Tomcat unable to obtain the corresponding domain name information.
When back-end Tomcat supports pan domain name resolution, you need to set host name to localhost to support generic domain name pointing.
Nginx Multi-domain Configuration
Nginx binding multiple domain names can also be a number of domain rules to write a configuration file, you can also set up a number of domain name configuration files, I generally for the convenience of management, each domain name to build a file, some of the same domain name can also be written in a general configuration file.
One, each domain name a file of the wording
First open the Nginx domain name configuration file directory:/usr/local/nginx/conf/servers, if you want to bind the domain name www.web126.com in this directory to build a file: www.web126.com.conf then write the rules in this file Such as:
server {listen; server_name www.web126.com; #绑定域名 index index.htm index.html index.php; #默认文件 root/home/www/web126.com; #网站根目录 include location.conf; #调用其他规则, can also be removed}
Then re-start the Nginx server, the domain name is bound successfully.
Nginx Server Restart command:/etc/init.d/nginx restart.
Second file multiple domain name notation
A file to add more than one domain name rule is the same, as long as the above a single domain name repeatedly written down is OK, such as:
server { listen 80; server_name www.web126.com; #绑定域名 index index.htm index.html index.php; #默认文件 root /home/www/web126.com; #网站根目录 include location.conf; #调用其他规则, can also remove } server { listen 80; server_name msn.web126.com; #绑定域名 index index.htm index.html index.php; #默认文件 root /home/www/msn.web126.com; #网站根目录 include location.conf; #调用其他规则, can also remove }
Third, not with the WWW domain name plus 301 Jump
If the domain name without the www to add 301 jump, it is also bound to the same domain name, the first binding without the WWW domain name, just do not write the site directory, but do 301 jump, such as:
server {listen; server_name web126.com; rewrite ^/(. *) http://www.web126.com/$1 Permanent;}
Iv. Add 404 pages
Add 404 pages that can be added directly inside, such as:
server {listen; server_name www.web126.com; #绑定域名 index index.htm index.html index.php; #默认文件 root/home/www/web126.com; #网站根目录 include location.conf; #调用其他规则, can also remove error_page 404/404.html; }
Finally, there is a way to note that there may be a need to prohibit IP direct access to port 80 or prohibit the non-site domain name binding our IP, so that should
To do this, put it on top of the previous server:
server{listen default; server_name _; return 403;}
Learn the above four rules method, the basic can independently solve the Nginx multi-domain name configuration problem.
Nginx Multi-server Reverse proxy configuration