2013-07-13 37 views
0

我試圖建立與基本3種不同類型的內容的nginx的服務器:如何在nginx上的論壇子文件夾中提供靜態文件?

  • 主要網站上,笨運行
  • AQ &論壇,在子文件夾/qa(上Question2Answer運行)
  • 靜態文件(在不同的地點,包括/qa

我遇到各種麻煩。我現在的配置(服務器塊內)是:

# Q2A 
if ($request_uri ~* "^/qa/") { 
    rewrite ^/qa/(.*)$ /qa/index.php?qa-rewrite=$1 last; 
} 
# CI 
if (!-e $request_filename) { 
    rewrite ^(.+)$ /index.php?$1 last; 
} 
location/{ 
    index index.php index.html; 
} 
location ~ \.php$ { 
     try_files $uri =404; 

     fastcgi_cache one; 
     fastcgi_cache_key $scheme$host$request_uri; 
     fastcgi_cache_valid 200 302 304 5m; 
     fastcgi_cache_valid 301 1h; 

     include /etc/nginx/fastcgi_params; 
     fastcgi_pass unix:/var/run/php-fastcgi/php-fastcgi.socket; 
     fastcgi_index index.php; 
     fastcgi_param SCRIPT_FILENAME /srv/www/site$fastcgi_script_name; 
     fastcgi_param HTTPS off; 
} 

這主要工作,除了這些問題:

在我的應用程序文件夾中的PHP文件
  • 請求被分析/執行。顯然,因爲這不是通過CI應用程序它會導致錯誤(變量未找到等)。
  • qa夾中的所有靜態文件正在通行證Q2A應用,而被服務的靜態文件

我已經嘗試了很多不同的東西我已經記不清,如使用位置塊像location ~* ^/qa/ {}try_files的各種排列,但沒有運氣。我也嘗試修改this Wordpress example on the nginx site。他們中的大多數只是以/qa/返回404結束。一些方法導致服務器提供原始PHP代碼!

任何人都可以幫助正確的方法來設置?

回答

1

替換

if ($request_uri ~* "^/qa/") { 
    rewrite ^/qa/(.*)$ /qa/index.php?qa-rewrite=$1 last; 
} 

location ~ /qa/(.*)? { 
    try_files $uri /qa/index.php?qa-rewrite=$1&$query_string; 
} 

也塊

if (!-e $request_filename) { 
    rewrite ^(.+)$ /index.php?$1 last; 
} 

是更好的/位置內被移動,並轉換成try_files

location/{ 
    index index.php index.html; 
    try_files $uri /index.php?$request_uri 
} 

如果您仍有問題請告訴我。

+0

謝謝,但由於某種原因,除了目錄本身'mysite.com/qa /'之外,/ qa目錄中的頁面不工作。另外,我嘗試將其改爲'location〜* ^/qa /(.*)?$'以確保它是整個URL,並提示下載PHP代碼! – DisgruntledGoat

+0

嗯,我可能會有一些錯誤。這裏是最初的Apache規則:'RewriteRule ^。* $ index.php?qa-rewrite = $ 0&%{QUERY_STRING} [L]' – DisgruntledGoat

+0

就像我說的,如果我使用'location〜'** **下載**實際的PHP代碼,即下載文件'/ qa/index.php'。爲什麼會這樣? – DisgruntledGoat

0

If is evil。但是你可以使用try_files和一些位置塊來完成同樣的事情。

# in a `server` block 
index index.php index.html; 

# case sensitive version 
# location ~ ^/qa/(.*)?$ { 
location ~* ^/qa/(.*)?$ { 
    try_files $uri /qa/index.php?qa-rewrite=$1; 
} 

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

# not sure if you even need location /, this might work 
# try_files $uri /index.php?$request_uri; 

# the rest of your FastCGI config stuff here 
+0

這不起作用,在qa文件夾中,它提示下載PHP代碼! – DisgruntledGoat

0

這是基於我用於運行nginx的PHP站點的配置。

請注意,這是未經測試,但它應該工作,因爲它只是一個稍微修改的版本。

只需將(插入)替換爲日誌和根指令中的服務器值即可。

server { 
    listen 80; 
    access_log /var/log/nginx/(insert).access.log; 
    error_log /var/log/nginx/(insert).error.log; 
    root (insert); 
    server_name (insert); 
    rewrite ^/qa/(.*(?![\.js|\.css])[^.]+)$ /qa/index.php/$1 last; 
    rewrite ^(.*(?![\.js|\.css])[^.]+)$ /index.php/$1 last; 
    location ~ [^/]\.php(/|$) { 

     fastcgi_cache one; 
     fastcgi_cache_key $scheme$host$request_uri; 
     fastcgi_cache_valid 200 302 304 5m; 
     fastcgi_cache_valid 301 1h; 

     fastcgi_split_path_info ^(.+?\.php)(/.*)$; 
     fastcgi_pass unix:/var/run/php-fastcgi/php-fastcgi.socket; 
     fastcgi_index index.php; 
     include fastcgi_params; 
    } 
}