Home  >  Q&A  >  body text

Reverse proxy - how to configure nginx with the same IP, multiple domain names, and different ports?

That is, I have a domain name ABC.com
One server and two website applications
The two website applications are hung on different ports
I have set up two domain names, A.ABC.com, B.ABC.com
Accessing A.ABC.com and B.ABC.com can point to these two applications.
How to configure

阿神阿神2713 days ago864

reply all(4)I'll reply

  • 世界只因有你

    世界只因有你2017-05-16 17:19:48

    Similar to this

    server { 
    listen       80; 
    server_name  A.ABC.com; 
    location / { 
    proxy_pass http://localhost:1234; 
    proxy_set_header   Host    $host; 
    proxy_set_header   X-Real-IP   $remote_addr; 
    proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for; 
        } 
    } 

    Another one:

    server { 
    listen       80; 
    server_name  B.ABC.com; 
    location / { 
    proxy_pass http://localhost:4321; 
    proxy_set_header   Host    $host; 
    proxy_set_header   X-Real-IP   $remote_addr; 
    proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for; 
        } 
    } 

    In this way, the two requests can be forwarded to the corresponding local program ports. . . :)

    reply
    0
  • 某草草

    某草草2017-05-16 17:19:48

    You need to configure the virtual host so that Nginx listens to port 80 of different domain names and then forwards it to the actual port of the respective application

    First, you need to edit the /etc/nginx/nginx.conf,在httpmodule to introduce other configuration files:

    include /etc/nginx/conf.d/*.conf;

    This way you can set up each virtual host individually in a /etc/nginx/conf.d folder.

    Then create new files in the above folders /etc/nginx/conf.d/a.conf/etc/nginx/conf.d/b.conf,当然文件名ab It’s up to you.

    server {
        listen       80;
        server_name  a.abc.com;
    
        access_log /data/node/log/host.access.log  main;
    
        location / {
            proxy_set_header Host $http_host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Real-Ip $remote_addr;
            proxy_set_header X-NginX-Proxy true;
            proxy_pass http://127.0.0.1:<YOUR PORT>/;
            proxy_redirect off;
        }
    }

    Yesb.abc.com的应用只需要修改上面的server_nameThat’s it.

    In this way, each request to access http://a.abc.com will be forwarded to the corresponding port and processed by the respective application.

    reply
    0
  • 巴扎黑

    巴扎黑2017-05-16 17:19:48

    The domain name points to the same IP and is equipped with a corresponding virtual host

    reply
    0
  • 某草草

    某草草2017-05-16 17:19:48

    You can use Nginx’s reverse proxy

    reply
    0
  • Cancelreply