NGINX: サブディレクトリを機能させる

NGINX: サブディレクトリを機能させる

最近、Apache 2.4 から NGINX 1.7.12 に変更しましたが、そのスピードと汎用性が気に入っています。残念ながら、まだ SERVER ブロックを正しく動作させることができず困っています。

私は PHP を提供するために FASTCGI php5-fpm を使用しています。私の NGINX のルートは /var/www です。MC と呼ばれるメインの Web サイトは www.example.com にあり、/var/www/mc から提供されています。

同時に、私はメインサイトwww.example.com/masmagazineのサブディレクトリとして「masmagazine」というWordPressブログを運営しており、/var/www/masmagazineから提供されています。

2 つの異なるサーバー ブロック ファイルを使用して、両方の Web サイト (メイン サイトとブログ) を動作させることに成功しましたが、両方の Web サイトを同時に動作させるためにそれらを正しく結合することができませんでした。

www.example.com の作業用サーバー ブロック:

server {
listen 80 default_server;
listen [::]:80 default_server;
server_name www.example.com;
root /var/www/mc;
index index.php index.html index.htm;
location / {
    try_files $uri $uri/ /index.php;
}   
location /includes {
    rewrite ^/includes/css/coupon-unit.css /includes/css/coupon-unit.php;
    rewrite ^/includes/css/mobile-menu.css /includes/css/mobile-menu.php;
    rewrite ^/includes/css/mobile.css /includes/css/mobile.php;
    rewrite ^/includes/css/style.css /includes/css/style.php;
    rewrite ^/includes/css/users.css /includes/css/users.php;
}           
location /feed {
    rewrite ^/feed/ /feed.php break;
}
location /internationalsitemap {
    rewrite ^/internationalsitemap.xml /internationalsitemap.php;
}
location /page {
    rewrite ^/page-sitemap.xml /sitemap.php?site=page;
    rewrite ^/page-child-sitemap.xml /sitemap.php?site=childstore;
}
location /newstores {
    rewrite ^/newstores-sitemap.xml /sitemap.php?site=store;
}
location /coupon_category {
    rewrite ^/coupon_category-sitemap.xml /sitemap.php?site=category;
}
location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}
}

www.example.com/masmagazine の作業用サーバー ブロック":

server {
listen 80 default_server;
listen [::]:80 default_server;
server_name www.example.com;
index index.php index.html index.htm;
root /var/www;

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

location /masmagazine {
    alias /var/www/masmagazine;
    try_files $uri $uri/ /masmagazine/index.php?$args;
}   

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

}

これら 2 つを組み合わせると、masmagazine で 404 が見つからないというエラーが発生します。これは、php のロケーション ブロックなど内でサブディレクトリ (masmagazine の) の状況を正しく処理していないためと思われます。

どのような助けでもいただければ大変ありがたいです。本当に困っています。

Apache では次の方法が機能しました:

<VirtualHost *:80>
ServerName http://www.example.com
DocumentRoot /var/www/mc
<Directory /var/www/mc/>
    Options FollowSymLinks
    AllowOverride All
    DirectoryIndex index.php index.html
    Require all granted
</Directory>

Alias /masmagazine /var/www/masmagazine

<Directory  /var/www/masmagazine/>

        Options FollowSymLinks

        AllowOverride All

        DirectoryIndex index.php index.html

        Require all granted

</Directory>

</VirtualHost>

アップデート:

Tero の提案に従って、次の変更されたサーバー ブロックを使用しました。

server {
listen 80 default_server;
listen [::]:80 default_server;
server_name www.example.com;
root /var/www/mc;
index index.php index.html index.htm;

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

location /masmagazine {
    alias /var/www/masmagazine;
    try_files $uri $uri/ /index.php?$args;
}

location /includes {
    rewrite ^/includes/css/coupon-unit.css /includes/css/coupon-unit.php;
    rewrite ^/includes/css/mobile-menu.css /includes/css/mobile-menu.php;
    rewrite ^/includes/css/mobile.css /includes/css/mobile.php;
    rewrite ^/includes/css/style.css /includes/css/style.php;
    rewrite ^/includes/css/users.css /includes/css/users.php;
}   

location /feed {
    rewrite ^/feed/ /feed.php break;
}

location /internationalsitemap {
    rewrite ^/internationalsitemap.xml /internationalsitemap.php;
}

location /page {
    rewrite ^/page-sitemap.xml /sitemap.php?site=page;
    rewrite ^/page-child-sitemap.xml /sitemap.php?site=childstore;
}

location /newstores {
    rewrite ^/newstores-sitemap.xml /sitemap.php?site=store;
}

location /coupon_category {
    rewrite ^/coupon_category-sitemap.xml /sitemap.php?site=category;
}

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

}

結果は、www.example.com/masmagazine を要求すると 404 エラーになります。ログには、PHP プロセッサが var/www/masmagazine/index.php で探す必要があるのに、var/www/mc/masmagazine/index.php から masmagazine の index.php を処理しようとしている (index ディレクティブに従って) ことが示されています。

ログの関連部分は次のとおりです。

2015/05/22 12:00:54 [debug] 2108#0: *1 http request line: "GET /masmagazine/ HTTP/1.1"
2015/05/22 12:00:54 [debug] 2108#0: *1 http uri: "/masmagazine/"
2015/05/22 12:00:54 [debug] 2108#0: *1 http args: ""
2015/05/22 12:00:54 [debug] 2108#0: *1 http exten: ""
2015/05/22 12:00:54 [debug] 2108#0: *1 test location: "/"
2015/05/22 12:00:54 [debug] 2108#0: *1 test location: "internationalsitemap"
2015/05/22 12:00:54 [debug] 2108#0: *1 test location: "newstores"
2015/05/22 12:00:54 [debug] 2108#0: *1 test location: "masmagazine"
2015/05/22 12:00:54 [debug] 2108#0: *1 test location: ~ "\.php$"
2015/05/22 12:00:54 [debug] 2108#0: *1 using configuration "/masmagazine"
2015/05/22 12:00:54 [debug] 2108#0: *1 http cl:-1 max:1048576
2015/05/22 12:00:54 [debug] 2108#0: *1 rewrite phase: 3
2015/05/22 12:00:54 [debug] 2108#0: *1 post rewrite phase: 4
2015/05/22 12:00:54 [debug] 2108#0: *1 generic phase: 5
2015/05/22 12:00:54 [debug] 2108#0: *1 generic phase: 6
2015/05/22 12:00:54 [debug] 2108#0: *1 generic phase: 7
2015/05/22 12:00:54 [debug] 2108#0: *1 access phase: 8
2015/05/22 12:00:54 [debug] 2108#0: *1 access phase: 9
2015/05/22 12:00:54 [debug] 2108#0: *1 access phase: 10
2015/05/22 12:00:54 [debug] 2108#0: *1 access phase: 11
2015/05/22 12:00:54 [debug] 2108#0: *1 post access phase: 12
2015/05/22 12:00:54 [debug] 2108#0: *1 try files phase: 13
2015/05/22 12:00:54 [debug] 2108#0: *1 http script var: "/masmagazine/"
2015/05/22 12:00:54 [debug] 2108#0: *1 trying to use file: "/" "/var/www/masmagazine/"
2015/05/22 12:00:54 [debug] 2108#0: *1 http script var: "/masmagazine/"
2015/05/22 12:00:54 [debug] 2108#0: *1 trying to use dir: "/" "/var/www/masmagazine/"
2015/05/22 12:00:54 [debug] 2108#0: *1 try file uri: "/masmagazine/"
2015/05/22 12:00:54 [debug] 2108#0: *1 content phase: 14
2015/05/22 12:00:54 [debug] 2108#0: *1 content phase: 15
2015/05/22 12:00:54 [debug] 2108#0: *1 open index "/var/www/masmagazine/index.php"
2015/05/22 12:00:54 [debug] 2108#0: *1 internal redirect: "/masmagazine/index.php?"
2015/05/22 12:00:54 [debug] 2108#0: *1 rewrite phase: 1
2015/05/22 12:00:54 [debug] 2108#0: *1 test location: "/"
2015/05/22 12:00:54 [debug] 2108#0: *1 test location: "internationalsitemap"
2015/05/22 12:00:54 [debug] 2108#0: *1 test location: "newstores"
2015/05/22 12:00:54 [debug] 2108#0: *1 test location: "masmagazine"
2015/05/22 12:00:54 [debug] 2108#0: *1 test location: ~ "\.php$"
2015/05/22 12:00:54 [debug] 2108#0: *1 using configuration "\.php$"
2015/05/22 12:00:54 [debug] 2108#0: *1 http cl:-1 max:1048576
2015/05/22 12:00:54 [debug] 2108#0: *1 rewrite phase: 3
2015/05/22 12:00:54 [debug] 2108#0: *1 post rewrite phase: 4
2015/05/22 12:00:54 [debug] 2108#0: *1 generic phase: 5
2015/05/22 12:00:54 [debug] 2108#0: *1 generic phase: 6
2015/05/22 12:00:54 [debug] 2108#0: *1 generic phase: 7
2015/05/22 12:00:54 [debug] 2108#0: *1 access phase: 8
2015/05/22 12:00:54 [debug] 2108#0: *1 access phase: 9
2015/05/22 12:00:54 [debug] 2108#0: *1 access phase: 10
2015/05/22 12:00:54 [debug] 2108#0: *1 access phase: 11
2015/05/22 12:00:54 [debug] 2108#0: *1 post access phase: 12
2015/05/22 12:00:54 [debug] 2108#0: *1 try files phase: 13
2015/05/22 12:00:54 [debug] 2108#0: *1 http script var: "/masmagazine/index.php"
2015/05/22 12:00:54 [debug] 2108#0: *1 trying to use file: "/masmagazine/index.php" "/var/www/mc/masmagazine/index.php"
2015/05/22 12:00:54 [debug] 2108#0: *1 trying to use file: "=404" "/var/www/mc=404"2015/05/22 12:00:54 [debug] 2108#0: *1 http finalize request: 404, "/masmagazine/index.php?" a:1, c:2
2015/05/22 12:00:54 [debug] 2108#0: *1 http special response: 404, "/masmagazine/index.php?"
2015/05/22 12:00:54 [debug] 2108#0: *1 http set discard body
2015/05/22 12:00:54 [debug] 2108#0: *1 xslt filter header
2015/05/22 12:00:54 [debug] 2108#0: *1 HTTP/1.1 404 Not Found

答え1

このブロックを最初のserverブロックに追加します。

location /masmagazine {
    alias /var/www/masmagazine;
    try_files $uri $uri/ /index.php?$args;
}

try_filesロケーション ブロックの内部は、ここではステートメントを使用して定義されているブロック ルート ディレクトリを基準として機能しlocationますalias

関連情報