Home  >  Article  >  Operation and Maintenance  >  How to bind nginx reverse proxy secondary domain name

How to bind nginx reverse proxy secondary domain name

PHPz
PHPzforward
2023-05-20 10:55:242138browse

1. Application scenarios

We often encounter situations where multiple web sites are established on the same server. A common approach is to configure different ports for different sites, so that You can access it in the form of ip:port.

But after all, using IP to access is inconvenient, not vivid, and not easy to remember. Then, we can bind different domain names to each site. (Here we only talk about the public network situation) Even if we only have one domain name, we can set up multiple second-level domain names, which is easy to achieve using nginx.

2. Basic requirements

Usually, online tutorials will directly post a piece of code for the configuration file, telling us that this is OK. But I found that many novices can't succeed like bloggers after looking at the tutorial configuration, so let's talk about the basic principles next.

First of all, you must have a domain name that you can manage. For example, if I have a domain name of postmsg.cn, I can generate multiple sub-domain names for thousands of generations...

Secondly, There is an accessible public network server. You can build your own web site on this server, one, two, three or four, with different corresponding ports.

Then, there is the binding of the domain name and the site, one carrot for each pit, of course, you can also have multiple carrots for one pit...

3. General configuration

There are two places that need to be configured. Let’s talk about the first prerequisites first, which are also areas that some novices tend to overlook.

  (1) Domain name configuration

     A record resolution of the domain name pointed to our public network server, this is something most people can think of. When parsing, you can only add IP, but not port number, that is, you can only use the default port 80. (Domain name forwarding is not discussed)

If you want to implement subdomain name binding, you must configure it properly during domain name resolution. Otherwise, relying only on nginx will not work.

For example, if I want to bind the subdomain name p.postmsg.cn to port 8001 of the server, I must first ensure that the request to access p.postmsg.cn can reach the server, and then nginx can process it.

At this time, either add a host record of p in the domain name a record resolution, or there is a wildcard * configuration in the host record (all accesses to *.postmsg.cn are resolved to the corresponding server, use with caution), Just setting @ and www will definitely not work.

How to bind nginx reverse proxy secondary domain name

##                                    It can be left to nginx to handle.

When modifying the nginx configuration, you can modify it directly in the default configuration file (such as /etc/nginx/nginx.conf, as shown in the following code snippet), or you can create a new independent configuration in a certain directory file, and then include the configuration file in this directory (as shown on line 17).

http {
  log_format main '$remote_addr - $remote_user [$time_local] "$request" '
           '$status $body_bytes_sent "$http_referer" '
           '"$http_user_agent" "$http_x_forwarded_for"';
  access_log /var/log/nginx/access.log main;
  sendfile      on;
  tcp_nopush     on;
  tcp_nodelay     on;
  keepalive_timeout  65;
  types_hash_max_size 2048;
  include       /etc/nginx/mime.types;
  default_type    application/octet-stream;
  include /etc/nginx/conf.d/*.conf;
}

This is the format for nginx to read configuration files. Our configuration is usually written in the http { } block, plus the server block, and configured in the server block. As mentioned just now, the server block can be written in a separate file and included in nginx.conf. At the same time, nested include is supported.

Next, let’s take a look at how the server block is written:

server {
  listen 80;
  server_name p.postmsg.cn;
  location / {
  proxy_pass http://127.0.0.1:8008;
    proxy_redirect off;
    proxy_set_header host $host;
    proxy_set_header x-real-ip $remote_addr;
    proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
  }
  #access_log logs/p_access.log;
}

The server_name here corresponds to the configured domain name, and proxy_pass corresponds to the real address of the proxy.

  (3) Note 1

Be sure to leave port 80 to nginx, which is responsible for the proxy. We can use apache httpd, nginx, and tomcat at the same time on one server, but domain name resolution can only use the default port 80, and the comrades in charge of the agency must get the first-hand requests.

 (4) Note 2

 Immediate effectiveness. Some friends feel that they have configured everything correctly and have restarted the service, but they just can't see the desired results. There are three possible factors here.

  

Domain name resolution effective time

. Alibaba Cloud's minimum domain name validity time is 10 minutes, and sometimes it may be longer than this value.
  1.   Local dns cache.

    You can use the ipconfig /flushdns command in cmd to refresh the local dns cache.
  2.   Browser cache.

    This may be the hardest to find, especially in Google Chrome. After f12, right-click the refresh button next to the address bar in the upper left corner, click Clear Cache and Hard Reload.
  3.   The service has not been restarted.

    service nginx restart Sometimes it may not be useful to stop first and then start.

The above is the detailed content of How to bind nginx reverse proxy secondary domain name. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete