Nginx

Nginx 目錄別名錯誤地傳遞給 fcgi

  • March 5, 2012

我有一個網站虛擬主機 example.com,它從 /home/eralms/www 靜態提供。

但是,該網站執行一系列 perl 腳本,這些腳本位於各個目錄中。所以我想要完成的是以下內容:

http://example.com/               -> /home/erealms/www
http://example.com/erealms        -> /home/erealms/ethereal/main
http://example.com/erealms/admin  -> /home/erealms/ethereal/mgmt/admin
http://example.com/erealms/config -> /home/erealms/ethereal/mgmt/config

在除http://example.com/之外的所有目錄中,都會有 perl 文件 .pl,通過 fcgiwrapper 提供。

這是我目前的配置:

server {
   add_header Cache-Control public;
   access_log /var/log/nginx/access.log main buffer=32k;
   error_log /var/log/nginx/error.log error;
   expires max;
   limit_req zone=gulag burst=200 nodelay;
   listen 80;
   server_name example.com;
   index index.html index.htm default.html default.htm;
   root /home/erealms/www;

   location ~* (\.jpg|\.png|\.css)$ {
       if ($http_referer !~ ^(http://rpn.ishikawa.sne.jp) ) {
           return 405;
       }
   }
   location = /favicon.ico {
       return 204;
   }
   location /erealms/config {
       root /home/erealms/ethereal/mgmt/config/;
       gzip off;
       include /etc/nginx/fastcgi_params;
       fastcgi_pass 127.0.0.1:8000;
       fastcgi_param SCRIPT_FILENAME /home/erealms/ethereal/mgmt/config$fastcgi_script_name;
   }
   location /erealms/admin {
       root /home/erealms/ethereal/mgmt/admin/;
       gzip off;
       include /etc/nginx/fastcgi_params;
       fastcgi_pass 127.0.0.1:8000;
       fastcgi_param SCRIPT_FILENAME /home/erealms/ethereal/mgmt/admin$fastcgi_script_name;
   }
   location /erealms {
       alias /home/erealms/ethereal/main;
       gzip off;
       include /etc/nginx/fastcgi_params;
       fastcgi_pass 127.0.0.1:8000;
       fastcgi_param SCRIPT_FILENAME /home/erealms/ethereal/main$fastcgi_script_name;
   }
}

你會注意到我有 root 和 alias 指令,因為我試圖弄清楚到底發生了什麼。不過,這很奇怪。我在下麵包含了請求的目錄,以及 nginx 告訴 fcgiwrapper 訪問它們的位置:

http://example.com/erealms        -> /home/erealms/ethereal/main/erealms
http://example.com/erealms/admin  -> /home/erealms/ethereal/mgmt/admin/erealms/admin
http://example.com/erealms/config -> /home/erealms/ethereal/mgmt/config/erealms/config

現在暫時,只是為了讓該死的東西為進一步測試工作,我剛剛創建了指向它們應該在的位置的惰性符號連結,但顯然這不是一個非常優雅的解決方案。如果有人能指出我正確的方向來修復目前的設置以使其正常工作,或者如果您可能對此配置有一個更優雅的解決方案的想法,我將非常感激。

不知道如何在 SF 上回答你自己的問題,但這是我最終做的:

server {
   add_header      Cache-Control public;
   access_log      /var/log/nginx/access.log main buffer=32k;
   error_log       /var/log/nginx/error.log error;
   expires         max;
   limit_req       zone=gulag burst=200 nodelay;
   listen          80;
   server_name     rpn.ishikawa.sne.jp;
   root            /home/erealms/www;
   index           index.html;
   location ~* (\.jpg|\.png|\.css)$ {
           if ($http_referer !~ ^(http://rpn.ishikawa.sne.jp) ) {
                   return 405;
           }
   }
   location = /favicon.ico {
           return 204;
   }
   location ~ /erealms/config(/.*\.pl)$ {
           alias   /home/erealms/ethereal/mgmt/config;
           gzip off;
           include /etc/nginx/fastcgi_params;
           fastcgi_pass unix:/tmp/cgi.sock;
           fastcgi_param SCRIPT_FILENAME /home/erealms/ethereal/mgmt/config/$1;
   }
   location ~ /erealms/admin(/.*\.pl)$ {
           alias   /home/erealms/ethereal/mgmt/admin;
           gzip off;
           include /etc/nginx/fastcgi_params;
           fastcgi_pass unix:/tmp/cgi.sock;
           fastcgi_param SCRIPT_FILENAME /home/erealms/ethereal/mgmt/admin/$1;
   }
   location ~ /erealms(/.*\.pl)$ {
           alias   /home/erealms/ethereal/main;
           gzip off;
           include /etc/nginx/fastcgi_params;
           fastcgi_pass unix:/tmp/cgi.sock;
           fastcgi_param SCRIPT_FILENAME /home/erealms/ethereal/main/$1;
   }
}

現在不幸的是,這導致了一些其他錯誤,我最終將使用 apache 後端通過 nginx 前端提供動態內容,但我認為如果其他人遇到類似問題,我會將其留在這裡。

引用自:https://serverfault.com/questions/366313