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)設定中,我能夠輕鬆建立別名來指向其中的項目,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

不確定您為什麼使用別名。嘗試一下,它尚未經過測試,但它至少應該讓您更接近您想要實現的目標。如果它不起作用,請詳細說明它不起作用的原因,並顯示適用的日誌和捲曲。

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

編輯 基於評論“一旦我在伺服器範圍內更改 root 指令,我的 WordPress 網站上就會收到 404,就好像它忽略了位置範圍上的 root 指令一樣。”你可以試試這個。當根目錄中有自訂 PHP 應用程式且子目錄中有 Wordpress 時,我會使用此技術。

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

靜態公司

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

相關內容