Nginx forwards the secondary domain name

Posted by y2yang on Wed, 01 Jan 2020 05:38:34 +0100

Nginx forwards the secondary domain name

Through the division and implementation of the company's domain name, nginx is used to issue the secondary domain name. Take the following example to record: purchased the lwenhao.com domain name on Alibaba cloud, including blog.lwenhao.com (personal blog) and file.lwenhao.com (file management). To access these two domains, you need to visit different projects,

The first case: deploy the project under a Tomcat (Port 8080).
The second case: deploy the project on two tomcat ports of 80809090 respectively.

In the first case, you need to access the same port and different projects

1, blog.lwenhao.com configuration

upstream blog {
 server localhost:8080;
}

server {
    listen       80;
    server_name  blog.lwenhao.com;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
       # root   html;
       # index  index.html index.htm;
       proxy_pass http://blog/blog/;
       proxy_set_header Host $host:$server_port;
       proxy_set_header X-Real_IP $remote_addr;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_set_header X-Forwarded-Scheme $scheme;
       proxy_connect_timeout 3;
       proxy_read_timeout 3;
       proxy_send_timeout 3;
       access_log off;
       break;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
 }
}

2, file.lwenhao.com configuration

upstream file {
 server localhost:8080;
}

server {
    listen       80;
    server_name  file.lwenhao.com;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
       # root   html;
       # index  index.html index.htm;
       proxy_pass http://file/file/;
       proxy_set_header Host $host:$server_port;
       proxy_set_header X-Real_IP $remote_addr;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_set_header X-Forwarded-Scheme $scheme;
       proxy_connect_timeout 3;
       proxy_read_timeout 3;
       proxy_send_timeout 3;
       access_log off;
       break;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
 }
}

In the second case, the inaccessible port

1, blog.lwenhao.com configuration

upstream blog {
 server localhost:8080;
}

server {
    listen       80;
    server_name  blog.lwenhao.com;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
       # root   html;
       # index  index.html index.htm;
       proxy_pass http://blog/blog/;
       proxy_set_header Host $host:$server_port;
       proxy_set_header X-Real_IP $remote_addr;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_set_header X-Forwarded-Scheme $scheme;
       proxy_connect_timeout 3;
       proxy_read_timeout 3;
       proxy_send_timeout 3;
       access_log off;
       break;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
 }
}

2, file.lwenhao.com configuration

upstream file {
 server localhost:9090;
}

server {
    listen       80;
    server_name  file.lwenhao.com;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
       # root   html;
       # index  index.html index.htm;
       proxy_pass http://file/file/;
       proxy_set_header Host $host:$server_port;
       proxy_set_header X-Real_IP $remote_addr;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_set_header X-Forwarded-Scheme $scheme;
       proxy_connect_timeout 3;
       proxy_read_timeout 3;
       proxy_send_timeout 3;
       access_log off;
       break;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
 }
}

Topics: Nginx Tomcat