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.
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; }
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; }
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.Local dns cache.
You can use the ipconfig /flushdns command in cmd to refresh the local dns cache.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.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!