[php] nginx 위치에서 접두사 URL을 다시 작성하십시오.

내 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 URL이 경우, HTTPS 2로 리디렉션 계속 /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를 통과 할 수 없음을 의미합니다.

문제를 해결하는 방법을 아는 사람이 있습니까?



답변

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


답변

두 개의 분리 된 서버 컨텍스트를 사용하는 옵션이 있으며 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;
    }
}


답변