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 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를 통과 할 수 없음을 의미합니다.

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

답변:


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

그러나 https로 리디렉션되도록 권장되는 정적 html 페이지 요청이 있습니다. 당황합니다
JordanLu

Typo는 http를 https로 리디렉션하는 간단한 솔루션이 있습니다://$server_name$request_uri;
Dlk

어떻게 쓰는지 알려주시겠습니까? @Dlk
JordanLu

btw, 매개 변수 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;
    }
}
당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.