在nginx位置重写前缀url


10

我的nginx配置文件是这样的:

server {
    listen 80;
    listen 443 ssl;
    server_name XXX.com;

    error_log  /log/nginx/xxx.com_error.log;
    access_log /log/nginx/xxx.com_access.log main;

    root /data/www/;
    index index.php index.html index.htm;

    location ~ \.php$ {
        add_header X-Frame-Options SAMEORIGIN;

        fastcgi_pass   127.0.0.1:9000;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

我们需要配置nginx以满足以下要求:

1,如果URL 没有前缀“ /api/mobile/index.php”,并且请求的端口是80,则将其重定向到https 2,如果URL 具有前缀“ /api/mobile/index.php”,请继续

因此,我在配置文件中添加了内容:

    location ~ ^(?!/api/mobile/index\.php).*$ {
        if ($server_port = "80") {
               return 301 https://$server_name$request_uri;
        }

        rewrite /* $server_name$reqeust_uri last;
    }

现在配置文件的内容是:

server {
    listen 80;
    listen 443 ssl;
    server_name XXX.com;

    error_log  /log/nginx/xxx.com_error.log;
    access_log /log/nginx/xxx.com_access.log main;

    root /data/www/;
    index index.php index.html index.htm;

    location ~ ^(?!/api/mobile/index\.php).*$ {
        if ($server_port = "80") {
               return 301 https://$server_name$request_uri;
        }

        rewrite /* $server_name$reqeust_uri last;
    }

    location ~ \.php$ {
        add_header X-Frame-Options SAMEORIGIN;

        fastcgi_pass   127.0.0.1:9000;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

比请求匹配第一个位置,将不匹配其他位置。

这意味着这些请求无法通过php cgi。

有谁知道如何解决这个问题?

Answers:


4

Nginx仅匹配一个位置。也将配置移到第一个位置。

location ~ ^(?!/api/mobile/index\.php).*$ {
    if ($server_port = "80") {
           return 301 https://$server_name$request_uri;
    }

    add_header X-Frame-Options SAMEORIGIN;

    fastcgi_pass   127.0.0.1:9000;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

location ~ \.php$ {
    add_header X-Frame-Options SAMEORIGIN;

    fastcgi_pass   127.0.0.1:9000;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

但是,建议将一些静态html页面请求重新定向到https。不好意思
JordanLu

错字有一个简单的解决方案,像这样将http重定向到https://$server_name$request_uri;
Dlk

你能告诉我怎么写吗?@Dlk
JordanLu

顺便说一句,您可以通过使用named位置而不是复制fastcgi参数来改善这一点。
tftd

0

可以选择使用两个分开的服务器上下文,并且不使用if语句(在此处阅读原因:https : //www.nginx.com/resources/wiki/start/topics/depth/ifisevil/)。

配置可以是:

server {
    listen 80;
    server_name XXX.com;

    error_log  /log/nginx/xxx.com_error.log;
    access_log /log/nginx/xxx.com_access.log;

    root /data/www;
    index index.php index.html index.htm;

    location /api/mobile/index.php {
        rewrite ^(.*)$ https://$host$1 redirect;
    }

    location ~ \.php$ {
        add_header X-Frame-Options SAMEORIGIN;

        fastcgi_pass  127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include       fastcgi_param;
    }
}

server {
    listen 443 ssl http2;
    server_name XXX.com;

    error_log  /log/nginx/xxx.com_ssl_error.log;
    access_log /log/nginx/xxx.com_ssl_access.log;

    root /data/www;
    index index.php index.html index.htm;

    location ~ \.php$ {
        add_header X-Frame-Options SAMEORIGIN;

        fastcgi_pass  127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include       fastcgi_param;
    }
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.