Nginx ServerName Configuration Instructions

Source: Internet
Author: User
Tags vps nginx server

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:

?
1
?
12345 server {listen 80;server_name ssdr.info www.ssdr.info;...}

2. A string starting with a * wildcard:
?
1
?
12345 server {listen 80;server_name *.ssdr.info;...}

3. A string ending with a * wildcard character:
?
1
?
12345 server {listen 80;server_name www.*;...}

4. Match Regular expression:
?
1
?
12345 server {listen 80;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:
?
1
?
1234567 server{listen 80;server_name ~^(www\.)?(.+)$;index index.php index.html;root /data/wwwsite/$2;}

The home directory of the site should resemble this structure:
?
1
/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:
?
1
?
12345678910 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:
?
1
/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:
?
1
?
12345678910111213 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;}}

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:
?
1
?
12345678910111213 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;}

Let's revise the following.
?
1
?
123456789101112131415161718 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
?
1
?
1234567891011121314151617181920212223242526 ## 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:
?
1
?
12345678 server{listen 80;server_name www.web126.com; #绑定域名index index.htm index.html index.php; #默认文件root /home/www/web126.com; #网站根目录include location.conf; #调用其他规则,也可去除}

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:
?
1
?
12345678910111213141516 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:
?
1
?
123456 server{listen 80;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:
?
1
?
123456789 server{listen 80;server_name www.web126.com; #绑定域名index index.htm index.html index.php; #默认文件root /home/www/web126.com; #网站根目录include location.conf; #调用其他规则,也可去除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:
?
1
?
12345 server{listen 80default;server_name _;return403;}

Learn the above four rules method, the basic can independently solve the Nginx multi-domain name configuration problem.

Nginx ServerName Configuration Instructions (RPM)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.