Configuring proxy in nginx_ pass

Posted by turdferguson on Mon, 03 Jan 2022 08:48:29 +0100

nginx http proxy

Via proxy_set_header, which returns the real IP address and port of the client instead of the proxy host IP

server {
listen 80;
location / {
proxy_set_header Host h o s t : host: host:server_port; # Set request header: proxy IP: Port
proxy_set_header X-Real-IP $remote_addr; # Real client IP address
proxy_set_header X-Real-PORT $remote_port;
proxy_set_header X-Forwarded-For KaTeX parse error: Double subscript at position 12: proxy_add_x_̲forwarded_for; ...http_host:88$request_uri;
}
}

1, Configuring proxy in nginx_ Whether to add / when passing

Pay attention to proxy_ The last url after pass/
When / is added, which is equivalent to adding a path, nginx will not retain the matching path part in the location
If there is no /, the matching path part will be retained
Example:

location ^~ /static/css/
{
    proxy_cache css_cache;
    proxy_set_header Host css.ztit.cn;
    proxy_pass http://css.ztit.cn/;
}

As configured above
If the requested url is: http: / / [domain name] / static/css/a.css
Will be represented as: http://css.ztit.cn/a.css

location ^~ /static/css/
{
    proxy_cache css_cache;
    proxy_set_header Host css.ztit.cn;
    proxy_pass http://css.ztit.cn;
} 

As configured above
If the requested url is: http: / / [domain name] / static/css/a.css
Will be delegated to: http://css.ztit.cn/static/css/a.css

2, About proxy_ path problem of pass configuration

If, you don't want nginx to modify your URI request
So, proxy_ The configuration of pass should not have any path.
Example:

location /static/css/ {
    proxy_set_header Host $host;
    proxy_pass http://127.0.0.1:85;
}

If the requested url is: http://127.0.0.1/static/css/a.css
Will be delegated to: http://127.0.0.1:85/static/css/a.css

Otherwise, in proxy_ path is in the configuration of pass

location /static/css/ {
    proxy_set_header Host $host;
    proxy_pass http://127.0.0.1:85/path;
}

If the requested url is: http://127.0.0.1/static/css/a.css
Will be delegated to: http://127.0.0.1:85/path/a.css

nginx load balancing

Explain several state parameters of nginx load balancing configuration.

down indicates that the current server does not participate in load balancing temporarily.
Backup, reserved backup machine. When all other non backup machines fail or are busy, the backup machine will be requested, so the pressure on this machine is the least.
max_ Failures, the number of times a request is allowed to fail. The default value is 1. When the maximum number of times is exceeded, proxy is returned_ next_ Error in upstream module definition.
fail_timeout, after experiencing max_ The time the service is suspended after failures. max_ Failures can be compared with fail_ Use with timeout.

#Hot standby: if you have two servers, only when one server has an accident can you enable the second server to provide services. The order in which the server processes requests: AAAAA suddenly A hangs up, BBB

upstream images { 
    server 192.168.1.50:8080; 
    server 192.168.1.50:8080 backup;  #Hot standby     
}

#Polling: nginx is polling by default, and its weight is 1 by default. The order in which the server processes requests: ABAB

upstream images1 { 
    server 192.168.1.50:8080;
    server 192.168.1.51:8080;       
}

#Weighted polling: distribute different number of requests to different servers according to the configured weight. If not set, it defaults to 1. The request order of the following servers is: abbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabbabb

 upstream images2 { 
    server 192.168.1.50:8080 weight=1;
    server 192.168.1.51:8080 weight=2;
}

#ip_hash:nginx will make the same client IP request the same server.

upstream images3 { 
    server 192.168.1.50:8080; 
    server 192.168.1.51:8080;
    ip_hash;
}
upstream images4 {
    server 192.168.1.50:8080   max_fails=2 fail_timeout=30s;
    server 192.168.1.51:8080   max_fails=2 fail_timeout=30s;
}
server {
    listen 80;
    server_name _;

    location / {
        proxy_read_timeout 1800;
        proxy_next_upstream http_502 http_504 error timeout invalid_header;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_pass http://images;
    }
}

Topics: Load Balance Nginx