nginx.conf default server configuration:
server{ listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; }
When the location is configured, the subdirectory is preferred, and the default root directory is the last. For example, configure / ENT boot / first, so that if the user's request address is the path of / ENT boot / and nginx scans to this / ENT boot /, it will directly forward and no longer continue scanning configuration.
Where, proxy pass: refers to the proxy forwarding, which forwards the request to the specified url.
server { listen 9999; server_name localhost; location /ent-boot/ { proxy_pass http://192.168.40.84:8802/ent-boot/; } location / { root /www/back/; index index.html index.htm; } }
The proxy pass instruction is used to set the address of the proxied server. It can be in the form of host name, IP address and end slogan.
For example, the following configuration:
server { listen 80; server_name buguge.com www.buguge.com; location /proxy/ { proxy_pass ***; } }
When you visit http://bug.com/news/a.html,
- Proxy? Pass: http://127.0.0.1:9999/; proxy to URL: → http://127.0.0.1:9999/a.html
- Proxy? Pass: http://127.0.0.1:9999; proxy to URL: → http://127.0.0.1:9999/news/a.html
- Proxy? Pass: http://127.0.0.1:9999/article/; proxy to URL: → http://127.0.0.1:9999/p/a.html
- Proxy? Pass: http://127.0.0.1:9999/article; proxy to URL: → http://127.0.0.1:9999/article.html
In order to facilitate memory and specification configuration, it is recommended that all URLs after proxy pass end with "/". Reference: https://www.cnblogs.com/yyxianren/p/10831511.html
If you reverse proxy to a web site with a different domain name, you need to specify the Host value of the request header as that domain name, otherwise it will cause a dead cycle for the proxy. Reference: https://blog.csdn.net/bowei026/article/details/90417914
To achieve load balancing, use the upstream instruction.
upstream emei_server { ip_hash; server 10.0.2.75:9083 weight=10 max_fails=0; server 10.0.2.76:9083 weight=10 max_fails=0; } server{ ... location / { proxy_pass http://emei_server; proxy_set_header Host $host; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Real-IP $remote_addr; proxy_set_header remote-addr $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 100m; client_body_buffer_size 512k; proxy_connect_timeout 300; proxy_send_timeout 300; proxy_read_timeout 300; proxy_buffers 4 256k; proxy_buffer_size 256k; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404; proxy_busy_buffers_size 256k; } }
ip_hash: it can ensure that access to a server is handled by the same server, unless the assigned server is down. If the machine is down, the request is sent to other machines. It is applicable to the site that needs to log in and the session is only saved in its own node.
Reference: details of Nginx's upstream reverse proxy and load balancing https://www.cnblogs.com/heaprox/p/10132409.html
The strategies of nginx load balancing include polling, weight polling, IP hash, URL hash and so on. Reference: nginx configuration - upstream https://www.jianshu.com/p/ab7cf5484e8f