首页 > 解决方案 > proxy_pass 的位置不起作用,错误在哪里?

问题描述

它是通过 NGINX 指令重定向端口 3000 的简单 webservce location,但需要在proxy_pass. 我使用 UBUNTU 18 LTS 服务器,并逐步执行:

  1. sudo nano /etc/nginx/sites-available/myServce
  2. sudo nginx -t
  3. sudo service nginx restart

还有什么?似乎问题出在myServce NGINX 脚本上,我在下面复制。


配置

NGINX 配置位于/etc/nginx/sites-available/myServce,

server {
    root /var/www/myServce/html;
    index index.php index.html index.htm;
    server_name sub1.myServce.etc;

    location ~ ^/_sql.csv/(.*) {
      proxy_set_header Host $host;  # suppose sub1.myServce.etc
      proxy_set_header Accept 'text/csv';
      proxy_pass  http://localhost:3000/$1; # same when sub1.myServce.etc
    }
    location / {
        try_files $uri $uri/ =404;
    }
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        fastcgi_read_timeout 900;
    }
}

测试

参考测试,一切正常:

  1. curl http://myServce.etc:3000/
  2. curl http://myServce.etc:3000/t?limit=10

为了模拟 test2 上的预期结果,使用curl --header "Accept: text/csv" http://myServce.etc:3000/t?limit=10并且工作正常。

对已实现重定向器的类似测试,都是错误:

  1. curl http://myServce.etc/_sql.csv/返回 502 错误网关。
  2. curl http://myServce.etc/_sql.csv/t?limit=10返回 502 错误网关。

标签: nginx

解决方案


有效的解决方案,

location /_sql.csv/ {
   proxy_set_header Host $host;
   # add_header Content-Disposition 'attachment; filename="$request_filename.csv"';
   proxy_set_header Accept 'text/csv';
   proxy_pass http://localhost:3000/;
}

它绕过所有字符串之后_sql.csv


该解决方案在文件扩展名上创建了其他未解决的问题。


推荐阅读