php.inc

php.inc

私の Web サーバーには次のファイルがあります。

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

私はNginxを次のように設定しましたhttp://example.com/私のWordpressインストールに行きます。以前の(Apache)セットアップでは、アイテムを指すエイリアスを簡単に作成できましたmiscが、Nginxでこれを行う方法がわかりません。

    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

なぜエイリアスを使用しているのかわかりません。これを試してみてください。テストはされていませんが、少なくとも目的に近づくことができるはずです。うまくいかない場合は、なぜうまくいかないのかを詳しくコメントし、該当するログと curl を示してください。

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 が表示されます。これは、ロケーション スコープのルート ディレクティブが無視されているかのようです。」というコメントに基づいて、これを試してください。ルート ディレクトリにカスタム PHP アプリがあり、サブディレクトリに Wordpress がある場合に、この手法を使用します。

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

答え2

それでそれが判明したとしてlocation正規表現に基づくブロックは常に他のブロックを上書きします。location私の設定には2つの正規表現の場所がありました。静的リソースのキャッシュを有効にするブロックとPHPを有効にするブロックです。これらのブロックの1つは、配信されるすべてのコンテンツに一致したため、他のすべての場所ブロックは無視されていました。

私が最終的にやったことはネスト位置ブロック静的ファイルと PHP ブロックを location ブロック内に配置します。これで私の設定は次のようになります。

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

スタティック

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

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

関連情報