Nginx multi-Server reverse proxy configuration

Source: Internet
Author: User
Tags vps nginx server

Nginx multi-Server reverse proxy configuration

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 together.

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, and 3 are tomcat servers.

4. Independent tomcat

Front-end nginx

You can configure multiple virtual hosts for this deployment.

====================================
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

OpenThe directory where the nginx domain name configuration file is stored:/usr/local/nginx/conf/servers. to bind a domain name www.web126.com, create a file: www.web126.com. conf and then write rules in this file, such:

Server
{
Listen 80;
Server_namewww.web126.com; # bind a domain name
Index index.htm index.html index. php; # default file
Root/home/www/web126.com; # website root directory
Include location. conf; # Call other rules, which can also be removed
}

Then restart the nginx server, and the domain name is successfully bound.

Nginx server restart command:/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_namewww.web126.com; # bind a domain name
Index index.htm index.html index. php; # default file
Root/home/www/web126.com; # website root directory
Include location. conf; # Call other rules, which can also be removed
}

Server
{
Listen 80;
Server_namemsn.web126.com; # bind a domain name
Index index.htm index.html index. php; # default file
Root/home/www/msn.web126.com; # website root directory
Include location. conf; # Call other rules, which can also be removed
}

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_namewww.web126.com; # bind a domain name
Index index.htm index.html index. php; # default file
Root/home/www/web126.com; # website root directory
Include location. conf; # Call other rules, which can also be removed
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.


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.