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

NGINX can improve website performance and reliability by: 1. Process static content as a web server; 2. forward requests as a reverse proxy server; 3. allocate requests as a load balancer; 4. Reduce backend pressure as a cache server. NGINX can significantly improve website performance through configuration optimizations such as enabling Gzip compression and adjusting connection pooling.

NGINXserveswebcontentandactsasareverseproxy,loadbalancer,andmore.1)ItefficientlyservesstaticcontentlikeHTMLandimages.2)Itfunctionsasareverseproxyandloadbalancer,distributingtrafficacrossservers.3)NGINXenhancesperformancethroughcaching.4)Itofferssecur

NGINXUnit simplifies application deployment with dynamic configuration and multilingual support. 1) Dynamic configuration can be modified without restarting the server. 2) Supports multiple programming languages, such as Python, PHP, and Java. 3) Adopt asynchronous non-blocking I/O model to improve high concurrency processing performance.

NGINX initially solved the C10K problem and has now developed into an all-rounder who handles load balancing, reverse proxying and API gateways. 1) It is well-known for event-driven and non-blocking architectures and is suitable for high concurrency. 2) NGINX can be used as an HTTP and reverse proxy server, supporting IMAP/POP3. 3) Its working principle is based on event-driven and asynchronous I/O models, improving performance. 4) Basic usage includes configuring virtual hosts and load balancing, and advanced usage involves complex load balancing and caching strategies. 5) Common errors include configuration syntax errors and permission issues, and debugging skills include using nginx-t command and stub_status module. 6) Performance optimization suggestions include adjusting worker parameters, using gzip compression and

Diagnosis and solutions for common errors of Nginx include: 1. View log files, 2. Adjust configuration files, 3. Optimize performance. By analyzing logs, adjusting timeout settings and optimizing cache and load balancing, errors such as 404, 502, 504 can be effectively resolved to improve website stability and performance.

NGINXUnitischosenfordeployingapplicationsduetoitsflexibility,easeofuse,andabilitytohandledynamicapplications.1)ItsupportsmultipleprogramminglanguageslikePython,PHP,Node.js,andJava.2)Itallowsdynamicreconfigurationwithoutdowntime.3)ItusesJSONforconfigu

NGINX can be used to serve files and manage traffic. 1) Configure NGINX service static files: define the listening port and file directory. 2) Implement load balancing and traffic management: Use upstream module and cache policies to optimize performance.

NGINX is suitable for handling high concurrency and static content, while Apache is suitable for dynamic content and complex URL rewrites. 1.NGINX adopts an event-driven model, suitable for high concurrency. 2. Apache uses process or thread model, which is suitable for dynamic content. 3. NGINX configuration is simple, Apache configuration is complex but more flexible.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version
Visual web development tools
