How to bind nginx reverse proxy secondary domain name?

2023-11-13 20:45:37

1. Application scenarios

We often encounter the situation of establishing multiple web sites on the same server. The common practice is to configure different ports for different sites, so that they can be accessed in the form of ip:port.

But accessing with ip is inconvenient, not vivid and not easy to remember. Then, we can bind different domain names to each site. (We only talk about the public network here.) Even if we only have one domain name, we can set up multiple second-level domain names, which is easy to achieve with nginx.

2. Basic requirements

Usually, online tutorials will directly post a code of a configuration file and tell us that this is ok. But I found that many novices can't success like bloggers when they look at the course configuration, so let's talk about the basic principles.

First of all, you must have a domain name that you can manage. For example, if I have a domain name in postmsg.cn, I can give birth to multiple sub-domains, II, III and 10 million ...

Secondly, there is an accessible public network server, on which you can set up your own web site, one with two or three or four, and the corresponding ports are different.

Then, it is the binding of the domain name and the site. One radish is a pit, and of course, multiple radishes can be a pit ...

3. General configuration

There are two places that need to be configured. Let's talk about the first prerequisite first, which is also a place that some novices easily ignore.

(1) Domain name configuration

Most people can think of parsing the A record of the domain name to our public network server. When parsing, you can only add ip, not port number, that is, you can only use the default 80 port. (Do not discuss domain name forwarding)

If you want to realize addon domain, you must configure it properly when resolving domain names. Otherwise, nginx alone is not enough.

For example, if I want to bind the subdomain 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 handle it.

At this point, either add a P host record to the domain name A record resolution, or the host record has a wildcard * configuration (all accesses to *.postmsg.cn are resolved to the corresponding server, so be careful), and only setting @ and www is definitely not acceptable.

Figure 1-Example of Domain Name Resolution

(2) Host configuration

After the domain name request arrives at the host, everything can be handed over to nginx for processing.

When modifying nginx configuration, you can modify it directly in the default configuration file (for example, /etc/nginx/nginx.conf, as shown in the following code fragment), or you can create a new independent configuration file in a directory and then include the configuration file in this directory (as shown in 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 the configuration file. Our configuration is usually written in the http {} block, plus the server block, and configured in the server block. As I said just now, the server block can be written in a single file, which is included in nginx.conf, and at the same time, nested include is supported.

Let's take a look at the writing of the server block:

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

Here, server_name corresponds to the configured domain name, and proxy_pass corresponds to the real address of the agent.

(3) Note 1

Be sure to leave port 80 to nginx, who is in charge of the agent. We can use apache httpd, nginx and tomcat on one server at the same time, but the default port 80 can only be used for domain name resolution, and the comrades in charge of agency should get the first-hand request.

(4) Note 2

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

Effective time of domain name resolution. Alibaba Cloud's domain name has a minimum effective time of 10 minutes, which may sometimes be greater than this value.

Local dns cache. You can use ipconfig /flushdns command at cmd to refresh the local dns cache.

Browser cache. This is probably the least easy to find, especially Google browser. After f12, right-click the refresh button next to the address bar in the upper left corner, click Clear Cache and reload hard.

The service was not restarted. Service nginx start may sometimes be easy stop use without stopping first and then starting.


Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us