编程知识 cdmana.com

It's all caused by nginx prefix, fastdfs can't access!!

In the high concurrency scenario, the author has developed it , Simple to offer 、 Stable 、 Extensible delayed message queuing framework , It has precise timing task and delay queue processing function . Since the open source for more than half a year , It has successfully provided precise timing scheduling scheme for more than ten small and medium-sized enterprises , It has withstood the test of production environment . In order to benefit more children's shoes , Now give the open source framework address :

https://github.com/sunshinelyz/mykit-delay

PS: Welcome to Star Source code , It's fine too pr Your blazing code .

Write it at the front

Recently set up a set of FastDFS colony , The node is 6 individual . Unified external file upload and access services . In the actual development process , Front end children's shoes need to access other servers to forward to FastDFS Access to services , here , Front end access FastDFS An extra prefix has been added to the service , Lead to FastDFS Service return 400 Status code . that , How can we solve it ? The article has been included in :https://gitee.com/binghe001/technology-binghe and https://github.com/sunshinelyz/technology-binghe Guys, don't forget to give it to me Star yo !!

Problem recurrence

When building a cluster on the server , The unified external access interface is http://192.168.175.110, Front end children's shoes visit , It's through http://192.168.175.101 Access to the , And there's a prefix on the front file, That is, through http://192.168.175.101/file Visiting . The first thing I thought of was to pass the front-end request directly through Nginx Forwarding to 192.168.175.110 The server . stay 192.168.175.101 Simplified on the server Nginx The configuration is as follows .

upstream file {
    server 192.168.175.110:80 max_fails=3;
}

server {
    listen       80;
    server_name  192.168.175.101;
     location / {
  root   html;
  index  index.html index.htm;
  # allow cros Cross domain access  
  add_header 'Access-Control-Allow-Origin' '*';
  #proxy_redirect default;
  # Timeout for connection to proxy server , You have to pay attention to this time out Time cannot exceed 75 second , When a server fails , too 10 Seconds to another server .
  proxy_connect_timeout 10;
     }
    
   location ~/file {
 add_header 'Access-Control-Allow-Origin' '*';
 add_header 'Access-Control-Allow-Credentials' 'true';
 proxy_pass                  http://file;
 proxy_set_header Host $host:$server_port;
   }
}

I am here 192.168.175.110 Configured on the server Ngin As shown below .

server {
 listen       80;
 server_name  192.168.175.110;
 #charset koi8-r;
 #access_log  logs/host.access.log  main;
 location / {
     root   html;
     index  index.html index.htm;
     # allow cros Cross domain access  
     add_header 'Access-Control-Allow-Origin' '*';
     #proxy_redirect default;
     # Timeout for connection to proxy server , You have to pay attention to this time out Time cannot exceed 75 second , When a server fails , too 10 Seconds to another server .
     proxy_connect_timeout 10;
 }  
 location ~/group([0-9]){
  root /data/fastdfs/storage/data;
  ngx_fastdfs_module;
 }
}

here , There is a problem : adopt file The prefix forwards the request to 192.168.175.110 Server time , Returns the 400 Status code .

Actually , The location of the problem is relatively simple , It's just that the front-end access adds one more file Prefix . that , How can we solve this problem ?

Problem solving

In general ,Nginx Reverse proxy of , It will only replace the domain name or IP part , The rest will be forwarded as is . in other words , Front end access http://192.168.175.101/file when , Will be forwarded to http://192.168.175.110/file On , As a result, the file service interface cannot be accessed normally .

Now that we have the problem , Then our next step is to solve this problem . The idea is also simple , Is in the 192.168.175.101 When a request is received on the server file The prefix removed . How to get rid of ?

It's also very simple , Only need 192.168.175.101 On the server Nginx Add the following configuration to the .

location ^~/file/ {
 proxy_set_header Host $host;
 proxy_set_header  X-Real-IP        $remote_addr;
 proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
 proxy_set_header X-NginX-Proxy true;
 proxy_pass http://file/;
}

here ,192.168.175.101 On the server Nginx The configuration is as follows .

upstream file {
    server 192.168.175.110:80 max_fails=3;
}

server {
    listen       80;
    server_name  192.168.175.101;
     location / {
  root   html;
  index  index.html index.htm;
  # allow cros Cross domain access  
  add_header 'Access-Control-Allow-Origin' '*';
  #proxy_redirect default;
  # Timeout for connection to proxy server , You have to pay attention to this time out Time cannot exceed 75 second , When a server fails , too 10 Seconds to another server .
  proxy_connect_timeout 10;
     }
    
      location ^~/file/ {
        proxy_set_header Host $host;
        proxy_set_header  X-Real-IP        $remote_addr;
        proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://file/;
    }
}

here , Revisit http://192.168.175.101/file when , It was forwarded to http://192.168.175.110 On , Can correctly access the file service interface .

Knowledge expansion

nginx Reverse proxy configuration how to remove prefixes ?

Use Nginx When you're doing a reverse proxy , You can simply forward the request intact to the next service . Set up proxy_pass The request will only replace the domain name , If it's based on different url Suffixes to access different services , Then we need to do the following :

Method 1 : Add "/"

upstream pay {
  server localhost:8089 weight=5;
}
upstream order {
  server localhost:8090 weight=5;
}
server {
    listen              80;
    server_name         binghe.com;

    location ^~/pay/ {
        proxy_set_header Host $host;
        proxy_set_header  X-Real-IP        $remote_addr;
        proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://pay/;
    }

    location ^~/order/ {
        proxy_set_header Host $host;
        proxy_set_header  X-Real-IP        $remote_addr;
        proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://order/;
    }
}

^~/pay/ Indicates that the matching prefix is pay Request ,proxy_pass At the end of /, It will put /pay/* The back path is spliced directly to the back , Remove pay.

Method 2 :rewrite

upstream pay {
  server localhost:8089 weight=5;
}
upstream order {
  server localhost:8090 weight=5;
}

server {
    listen              80;
    server_name  binghe.com;

    location ^~/pay/ {
        proxy_set_header Host $host;
        proxy_set_header  X-Real-IP        $remote_addr;
        proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;

        rewrite ^/user/(.*)$ /$1 break;
        proxy_pass http://pay;
    }

    location ^~/order/ {
        proxy_set_header Host $host;
        proxy_set_header  X-Real-IP        $remote_addr;
        proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;

        rewrite ^/order/(.*)$ /$1 break;
        proxy_pass http://order;
    }
}

This article is from WeChat official account. - Glacier Technology (hacker-binghe)

The source and reprint of the original text are detailed in the text , If there is any infringement , Please contact the yunjia_community@tencent.com Delete .

Original publication time : 2020-12-02

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .

版权声明
本文为[glacier]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201224160737118k.html

Scroll to Top