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を設定する必要があります:

URLが1の場合は、しない接頭辞を持っている「/api/mobile/index.php",and要求のポートは80で、HTTPS 2にリダイレクトURLが場合は、持っている接頭辞を」上に行く/api/mobile/index.php",just

だから私は設定ファイルにコンテンツを追加します:

    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を通過できませんでした。

問題を解決する方法を知っている人はいますか?

回答:


4

Nginxは1つの場所のみに一致します。構成も最初の場所に移動します。

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;
}

しかし、httpsにリダイレクトされるように推奨された静的なhtmlページ要求がいくつかあります。それは恥ずかしいです
JordanLu

誤植httpをhttpsにリダイレクトする簡単なソリューションがあります://$server_name$request_uri;
Dlk

書き方を教えていただけませんか?@Dlk
JordanLu

ところで、パラメータnamedを複製する代わりに場所を使用することで、これを改善できますfastcgi
tftd

0

2つの別個のサーバーコンテキストを使用するオプションがあり、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;
    }
}
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.