php.inc

php.inc

내 웹 서버에 다음 파일이 있습니다.

/var/www/html/--+
                |
                +-misc--+
                |       |
                |       +-misc1--+
                |       |        |
                |       |        +-index.html
                |       |
                |       +-misc2--+
                |       |        |
                |       |        +-index.php
                |       |
                |       +-misc3.php
                |
                +-wordpress--+
                             |
                             +-index.php

Nginx를 다음과 같이 설정했습니다.http://example.com/내 Wordpress 설치로 이동합니다. 이전(Apache) 설정에서는 항목을 가리키는 별칭을 쉽게 만들 수 있었지만 miscNginx에서 이 작업을 수행하는 방법을 잘 모르겠습니다.

    index index.php index.html;
    root /var/www/html/wordpress;

    location ~ [^/]\.php(/|$) {
        limit_except GET POST {}
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        if (!-f $document_root$fastcgi_script_name) {
            return 404;
        }

        fastcgi_buffer_size 16k;
        fastcgi_buffers 16 16k;

        fastcgi_param    SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_param    PATH_INFO          $fastcgi_path_info;
        fastcgi_param    PATH_TRANSLATED    $document_root$fastcgi_path_info;
        fastcgi_param    SERVER_NAME        $host;
        fastcgi_param    HTTP_PROXY         "";

        fastcgi_pass unix:/var/run/php-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location ~* \.(?:css|js|jpg|jpeg|gif|png|mp4)$ {
        expires 1M;
        access_log off;
        add_header Cache-Control "public";
    }

    location / {
        try_files $uri $uri/ /index.php;
        limit_except GET {}
    }

내가 원하는 것은:

내가 시도한 것:

# http://example.com/misc1 shows index.html but anything else in the folder is 404
location /misc1 {
    alias /var/www/html/misc/misc1;
}

# http://example.com/misc1 gives 403, http://example.com/misc1/index.html gives 404
location /misc1/ {
    alias /var/www/html/misc/misc1;
}

# shows Wordpress 404 page
location /misc1/* {
    alias /var/www/html/misc/misc1;
}

# gives 403 error
location ~ /misc1 {
    alias /var/www/html/misc/misc1;
}

내가 시도한 어떤 것도 PHP에 영향을 미치지 않았으며 작동하지 않습니다.

답변1

왜 별칭을 사용하고 있는지 잘 모르겠습니다. 이것을 시도해 보십시오. 테스트되지는 않았지만 적어도 달성하려는 목표에 더 가까워질 것입니다. 작동하지 않는 경우 작동하지 않는 이유에 대한 세부정보를 댓글로 작성하고 해당 로그와 컬을 표시하세요.

root /var/www/html/misc
try_files $uri $uri/; # NB This can be specified at the server or location level

location / {
  root /var/www/html/wordpress;
  try_files $uri $uri/ /index.php?$args;
}

location /misc1/ {
  root /var/www/html/misc;
}

location /misc2/ {
  root /var/www/html/misc;
}

편집하다 "서버 범위에서 루트 지시문을 변경하자마자 내 Wordpress 사이트에 404가 표시됩니다. 마치 위치 범위에서 루트 지시문을 무시하는 것처럼 보입니다." 이것을 시도해 볼 수 있습니다. 저는 하위 디렉터리에 Wordpress가 있고 루트 디렉터리에 사용자 지정 PHP 앱이 있을 때 이 기술을 사용합니다.

root /var/www/html/;
location / {
  try_files $uri $uri/ /wordpress/index.php?$args;
}

답변2

그래서밝혀진 대로, location정규식 기반 블록은 항상 다른 location블록보다 우선합니다. 내 구성에는 두 개의 정규식 위치가 있습니다. 정적 리소스에 대한 캐싱을 활성화하기 위한 블록과 PHP를 활성화하기 위한 블록입니다. 이 블록 중 하나가 제공되는 모든 콘텐츠와 일치하므로다른 모든 위치 블록은 무시되었습니다!

내가 결국 한 일은중첩 위치 블록, 정적 파일과 PHP 블록을 위치 블록 안에 배치합니다. 이제 내 구성은 다음과 같습니다.

php.inc

location ~ [^/]\.php(/|$) {
    limit_except GET POST {}
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    if (!-f $document_root$fastcgi_script_name) {
        return 404;
    }

    fastcgi_buffer_size 16k;
    fastcgi_buffers 16 16k;

    fastcgi_param    SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param    PATH_INFO          $fastcgi_path_info;
    fastcgi_param    PATH_TRANSLATED    $document_root$fastcgi_path_info;
    fastcgi_param    SERVER_NAME        $host;
    fastcgi_param    HTTP_PROXY         "";

    fastcgi_pass unix:/var/run/php-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}

정적.inc

location ~* \.(?:css|js|jpg|jpeg|gif|png|mp4)$ {
    expires 1M;
    access_log off;
    add_header Cache-Control "public";
}

example.conf

index index.php index.html;
root /var/www/html/wordpress;

location / {
    try_files $uri $uri/ /index.php;
    limit_except GET {}
    include conf.d/php.inc;
    include conf.d/static.inc;
}

location /misc1 {
    root /var/www/html/misc/;
    include conf.d/static.inc;
}

location /misc2 {
    root /var/www/html/misc/;
    include conf.d/php.inc;
    include conf.d/static.inc;
}

location /misc3.php {
    root /var/www/html/misc/;
    include conf.d/php.inc;
}

관련 정보