AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 537634
Accepted
Steve Bennett
Steve Bennett
Asked: 2013-09-10 16:28:22 +0800 CST2013-09-10 16:28:22 +0800 CST 2013-09-10 16:28:22 +0800 CST

nginx 在 404 上提供备用位置

  • 772

我正在尝试按如下方式设置 nginx 配置:当收到如下请求时/tile/SteveCountryVic/1/2/3.png:

  1. 尝试将其传递给http://localhost:5005/1/2/3.png
  2. 如果是 404s,则将其传递给另一台服务器/tile/SteveCountryVic/1/2/3.png

这是我的配置,它不太工作:

server {
   listen 80;
   server_name localhost;   error_log  /tmp/nginx.error.log notice;
   access_log   /tmp/nginx.access.log;
   location /tile/SteveCountryVic/ {
        rewrite_log on;        
        #rewrite ^.*/(\d+)/(\d+)/(\d+).*$ /$1/$2/$3.png break;

        proxy_intercept_errors on;
        error_page 404 = @dynamiccycletour;        
        #proxy_set_header Host $http_host;
        #proxy_pass http://127.0.0.1:5005;
        proxy_redirect /tile/SteveCountryVic/ http://localhost:5005/;

   location @dynamiccycletour {
        rewrite_log on;
        #rewrite ^(\d+)/(\d+)/(\d+).*$ /tile/SteveCountryVic/$1/$2/$3.png break;
        proxy_pass http://115.x.x.x:20008;


   }

   location /tile/ {
        proxy_set_header Host $http_host;
        proxy_pass http://127.0.0.1:20008;


        proxy_cache my-cache;
        proxy_cache_valid  200 302  60m;
        proxy_cache_valid  404      1m;
    }
    ...

在此配置中,所有请求似乎都被重定向到代理服务器,但最终会提供图像。此外,错误日志包含以下行:

2013/09/10 09:44:11 [error] 564#0: *138 open() "/etc/nginx/html/tile/SteveCountryVic/13/7399/5027.png" failed (2: No such file or directory), client: 118.x.x.x, server: localhost, request: "GET /tile/SteveCountryVic/13/7399/5027.png?updated=15 HTTP/1.1", host: "mydomain.org"

如果不使用proxy_redirect,我使用rewriteand proxy_pass:

        rewrite ^.*/(\d+)/(\d+)/(\d+).*$ /$1/$2/$3.png break;
        proxy_pass http://127.0.0.1:5005;

然后现在我实际上在浏览器中看到了 404 消息(即,它们没有被截获)。

我的问题:

  1. 我究竟做错了什么?
  2. 为什么 nginx 在 /etc/nginx/html/... 中寻找文件?
  3. 有没有办法获得更多的日志信息(特别是为了更好地理解 proxy_redirect)?
nginx
  • 2 2 个回答
  • 17413 Views

2 个回答

  • Voted
  1. Best Answer
    Steve Bennett
    2013-09-10T23:41:09+08:002013-09-10T23:41:09+08:00

    替代版本,使用rewrite和proxy_pass表现完美 - 问题是另一台服务器返回 200 而不是 404。因此,为了完整起见,这里是工作配置:

    server {
       listen 80;
       server_name localhost;
       error_log  /tmp/nginx.error.log notice;
       access_log   /tmp/nginx.access.log;
       location /tile/SteveCountryVic/ {
            rewrite_log on;
            rewrite ^.*/(\d+)/(\d+)/(\d+.*)$ /$1/$2/$3 break;
    
            proxy_intercept_errors on;
            error_page 404 = @dynamiccycletour;
            proxy_set_header Host $http_host;
            proxy_pass http://127.0.0.1:5005;
      }
    
       location @dynamiccycletour {
            rewrite_log on;
            rewrite ^/(\d+)/(\d+)/(\d+.*)$ /tile/SteveCountryVic/$1/$2/$3 break;
            proxy_pass http://115.x.x.x:20008;
    
       }
    
    • 9
  2. that guy from over there
    2013-09-10T23:48:09+08:002013-09-10T23:48:09+08:00

    首先你没有root正确设置你的 - 指令 -> 这就是你得到 404 的原因 -> 这就是为什么所有请求都被重定向到你的 @dynamiccycletour (openstreetmap?)

    顺便说一句, /tile/ 和 /tile/SteveCountryVic/ 有什么区别?

    所以我们首先需要在这里进行一些清理:

    server {
       ....
       # define where to find files 
       # be sure to have it like /path/to/tile
       root /path/to/tiles/;
    
    
       location /tile/SteveCountryVic/ {
    
           # if file not found -> remote server
           try_files $uri @dynamiccycletour
    
            rewrite_log on;        
            # this should cover /1/2/3.png. no?
            rewrite /tile/SteveCountryVic/(.*).png$ /$1.png break;
    
            # i'm not sure this will match due the the rewrite
            proxy_redirect /tile/SteveCountryVic/ http://localhost:5005/;
    
    
       location @dynamiccycletour {
            rewrite_log on;
    
            # this should cover /1/2/3.png. no?
            rewrite /tile/SteveCountryVic/(.*).png$ /$1.png break;
            proxy_pass http://115.x.x.x:20008;
    
    
       }
    
     }
    
    • 3

相关问题

  • Gzip 与反向代理缓存

  • nginx 作为代理的行为

  • Nginx 学习资源 [关闭]

  • 提供 70,000 个静态文件 (jpg) 的最佳方式?

  • 在 Apache、LightTPD 和 Nginx Web 服务器上提供 PHP 5.x 应用程序的现状?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve