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 / 问题 / 1112792
Accepted
CDuv
CDuv
Asked: 2022-10-12 02:18:27 +0800 CST2022-10-12 02:18:27 +0800 CST 2022-10-12 02:18:27 +0800 CST

将所有 URL 重定向到除一个路径之外的其他(外部)站点?

  • 772

我有一个工作网站(http://www.example.com)(使用 PHP-FPM),我想将所有传入流量重定向到另一个网站(ttps://www.other-example.com/foo.html)上的 URL,但一个特定路径()除外/api。

这是我尝试过的 Nginx 配置(将 PHP 处理的东西放在专用location的 for/api和return 301on 中location /):

upstream php {
    server unix:/var/run/php/php7.4-fpm.sock;
}

server {
    listen 80;
    listen [::]:80;

    server_name www.example.com;

    root /var/www/www.example.com;
    index index.php;

    location  ~ ^/api(?:/(.*))?$ {
        #return 418;
        try_files $uri $uri/ /index.php$is_args$args;
    
        location ~ \.php$ {
            #include snippets/fastcgi-php.conf;

            # regex to split $uri to $fastcgi_script_name and $fastcgi_path
            fastcgi_split_path_info ^(.+?\.php)(/.*)$;

            # Check that the PHP script exists before passing it
            try_files $fastcgi_script_name =404;

            # Bypass the fact that try_files resets $fastcgi_path_info
            # see: http://trac.nginx.org/nginx/ticket/321
            set $path_info $fastcgi_path_info;
            fastcgi_param PATH_INFO $path_info;

            fastcgi_index index.php;
            include fastcgi.conf;
            include fastcgi_params;

            fastcgi_pass php;
        }
    }

    location / {
        return 301 https://www.other-example.com/foo.html;
    }
}

但是我所有的请求(http://www.example.com和http://www.example.com/foo)http://www.example.com/api都会得到一个HTTP/1.1 301 Moved Permanentlywith Location: https://www.other-example.com/foo.html。

我知道这些location ~ ^/api(?:/(.*))?$作品,因为如果我取消注释,return 418;我会收到 418http://www.example.com/api个请求响应,但其他请求会收到 301 个响应。

redirect nginx
  • 1 1 个回答
  • 18 Views

1 个回答

  • Voted
  1. Best Answer
    Tero Kilkanen
    2022-10-12T15:06:50+08:002022-10-12T15:06:50+08:00

    在您的情况下,nginx 处理如下:

    1. 请求/api/test_endpoint匹配location ~ ^/api(?:/(.*))?$块。
    2. 在try_files中,它与/index.php$is_args$args零件相匹配。由于它是最后一部分,它会触发内部重定向。
    3. nginx 使用 uri 重新开始处理/index.php。这匹配location /块,触发重定向。

    解决该问题的一种解决方案是以下配置:

    location  ~ ^/api(?:/(.*))?$ {
        #return 418;
        try_files $uri $uri/ /index.php$is_args$args;
    }
    
    location / {
        return 301 https://www.other-example.com/foo.html;
    }
    
    location = /index.php {
        internal;
        #include snippets/fastcgi-php.conf;
    
        # regex to split $uri to $fastcgi_script_name and $fastcgi_path
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    
        # Check that the PHP script exists before passing it
        try_files $fastcgi_script_name =404;
    
        # Bypass the fact that try_files resets $fastcgi_path_info
        # see: http://trac.nginx.org/nginx/ticket/321
        set $path_info $fastcgi_path_info;
        fastcgi_param PATH_INFO $path_info;
    
        fastcgi_index index.php;
        include fastcgi.conf;
        include fastcgi_params;
    
        fastcgi_pass php;
    }
    

    index.php使用此配置,nginx会在请求处理的第 3 步中找到匹配项。该internal关键字阻止外部请求/index.php匹配此块。外部请求/index.php将按location /块提供。

    • 0

相关问题

  • 301 将根域重定向到 Godaddy Windows 主机帐户上的 www 子域

  • 使用 IIS7 重定向 Godaddy 中的子域

  • apache重写以将文件夹分配给域

  • 如何让 Apache2 重定向到子目录

  • 如何强制我的网址始终以 www 开头?

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