Nginx distribute different ports according to domain name

Copyright: code word is not easy, please indicate the source ~~ https://blog.csdn.net/m0_37190495/article/details/91442671
Reproduced, please indicate the source:
Original starting in: http://www.zhangruibin.com
article from RebornChang's blog

Distribution according to different ports domain

Demand background

Bloggers have bought two domain names, but only to buy a host, only a 80-port, but do not want to port with access time of the visit, so I think a different port access requests through a different domain name.

Implementation nginx proxy distribution

On a host deployed two service instances, respectively 81 and 82 ports. After domain DNS resolution access port 80 is designated server IP, so the implementation is carried out in the domain name service DNS resolution time, 80-port access to the specified server, using the information NGINX listening on port 80, if it is zhangruibin.com forwards 82 to the port, if it is 92cnb.com forwarded to port 81.

Installation NGINX

NGINX installation is very simple, there are many online tutorials are not mentioned here, can refer Bowen: https://www.cnblogs.com/kaid/p/7640723.html.

Note: 1. Install NGINX host, port 80 must be free, because the port is occupied NGINX default port 80. Otherwise it will error on startup of NGINX.
2. deployed service corresponding port must be open, look at the system using Firewalls or iptables.

Configuration NGINX

Edit the conf folder NGINX following nginx.conf file

vim /usr/local/nginx/conf/nginx.conf

Because bloggers two domain names are to be distributed, it is provided: worker_processes 2;
additionally required copy server {} and two modified contents inside server {}:

  server {
       listen       80;
       server_name  www.92cnb.com 92cnb.com ;

       location / {
           # 反向代理到 81 端口
           proxy_pass http://127.0.0.1:81;
           add_header Access-Control-Allow-Origin *;
           root   html;
           index  index.html index.htm;

            }
       error_page   500 502 503 504  /50x.html;
       location = /50x.html {
           root   html;
         }

          
       }
   server {
       listen       80;
       server_name  www.zhangruibin.com zhangruibin.com;

       #charset koi8-r;

       #access_log  logs/host.access.log  main;

       location / {
           # 反向代理到 82 端口
           proxy_pass http://127.0.0.1:82;
           add_header Access-Control-Allow-Origin *;
           root   html;
           index  index.html index.htm;
       }

       #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;
       }
   }

After saving the nginx reload the configuration file: ./ nginx -s reload
this time to visit the domain name, it is distributed to the designated port.
Results as shown below:


Over!

Guess you like

Origin blog.csdn.net/m0_37190495/article/details/91442671