2014-02-27 57 views
0

我有一個目錄/ web應用程序位於我的網站的Web根目錄之外。帶別名的嵌套位置

說,該網站是在這裏:

/var/www/site/htdocs/ 

和外部應用程序位於:

/var/www/apps/coolapp/ 

我的問題是我如何配置nginx的映射/路由就像www.mysite.com/coolapp/*所有請求(星號是通配符)到外部位置/var/www/apps/coolapp/?例如,www.mysite.com/coolapp/test.php應該服務器/var/www/apps/coolapp/test.php

我在production.conf指令中增加了一個alias指令,即主要nginx.conf文件包含的指令。這適用於除.php文件之外的所有內容,因爲另外一個location可以捕獲.php文件。所以我嵌入locationalias來捕獲.php文件,但是現在nginx告訴我它找不到.php文件「404 File Not Found」。以下是目前production.conf貌似

server { 
    listen 80; 
    listen 443 ssl; 

    ssl_certificate  /blah/blah/blah; 
    ssl_certificate_key /blah/blah/blah; 
    ssl_protocols  blah blah blah; 
    ssl_ciphers   blahblahblah; 
    ssl_prefer_server_ciphers blahblah; 

    access_log /var/log/nginx/www.mysite.com-access.log; 
    error_log /var/log/nginx/www.mysite.com-error.log error; 

    server_name mysite.com www.mysite.com; 
    root /var/www/site/htdocs; 

    include conf/magento_rewrites.conf; 
    include conf/magento_security.conf; 
    include /var/www/site/nginx/*.conf; 

    #-------CODE IN QUESTION------- 
    location /coolapp/ { 
     alias /var/www/apps/coolapp/; 
     location ~ \.php { 
      # Copied from "# PHP Handler" below 
      fastcgi_param MAGE_RUN_CODE default; 
      fastcgi_param MAGE_RUN_TYPE store; 
      fastcgi_param HTTPS $fastcgi_https; 
      rewrite_log on; 

      # By default, only handle fcgi without caching 
      include conf/magento_fcgi.conf; 
     } 
    } 

    # PHP handler 
    location ~ \.php { 
     ## Catch 404s that try_files miss 
     if (!-e $request_filename) { rewrite//index.php last; } 

     ## Store code is defined in administration > Configuration > Manage Stores 
     fastcgi_param MAGE_RUN_CODE default; 
     fastcgi_param MAGE_RUN_TYPE store; 
     fastcgi_param HTTPS $fastcgi_https; 
     rewrite_log on; 

     # By default, only handle fcgi without caching 
     include conf/magento_fcgi.conf; 
    } 

    # 404s are handled by front controller 
    location @magefc { 
     rewrite ^(.*) /index.php?$query_string last; 
    } 

    # Last path match hands to magento or sets global cache-control 
    location/{ 
     ## Maintenance page overrides front controller 
     index index.html index.php; 
     try_files $uri $uri/ @magefc; 
     expires 24h; 
    } 
} 

的conf/magento_fcgi.conf看起來是這樣的:

fastcgi_pass phpfpm; 

## Tell the upstream who is making the request 
proxy_set_header Host $host; 
proxy_set_header X-Real-IP $remote_addr; 
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
proxy_redirect off; 

# Ensure the admin panels have enough time to complete large requests ie: report generation, product import/export 
proxy_read_timeout 1600s; 

# Ensure PHP knows when we use HTTPS 
fastcgi_param HTTPS   $fastcgi_https; 

## Fcgi Settings 
include      fastcgi_params; 
fastcgi_connect_timeout  120; 
fastcgi_send_timeout   320s; 
fastcgi_read_timeout   1600s; 
fastcgi_buffer_size   128k; 
fastcgi_buffers 512   64k; 
fastcgi_busy_buffers_size  128k; 
fastcgi_temp_file_write_size 256k; 
fastcgi_intercept_errors  off; 
fastcgi_index index.php; 
fastcgi_param SCRIPT_FILENAME /var/www/apps/coolapp$fastcgi_script_name; 
fastcgi_param SCRIPT_NAME  $fastcgi_script_name; 
# nginx will buffer objects to disk that are too large for the buffers above 
fastcgi_temp_path    /tmpfs/nginx/tmp 1 2; 
#fastcgi_keep_conn    on; # NGINX 1.1.14 
expires      off; ## Do not cache dynamic content 

這裏有一些錯誤的消息,我從是error.log

2014/02/28 11:10:17 [error] 9215#0: *933 connect() failed (111: Connection refused) while connecting to upstream, ................. client: x.x.x.x, server: mysite.com, request: "GET /coolapp/test.php HTTP/1.1", upstream: "fastcgi://[::1]:9000", host: "www.mysite.com" 
2014/02/28 11:10:17 [error] 9215#0: *933 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: x.x.x.x, server: mysite.com, request: "GET /coolapp/test.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.mysite.com" 
2014/02/28 11:11:59 [error] 9220#0: *1193 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: x.x.x.x, server: mysite.com, request: "GET /coolapp/test.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.mysite.com" 

有誰拉看看我做錯了什麼?

+0

我們看不到有什麼問題,因爲它可能在'mage_rewrites.conf'中。 – Melvyn

+0

經過一些測試之後,似乎''uri'在使用別名指令時不會更新:'$ uri'仍然會以'/ coolapp /'作爲前綴。這可能是一個錯誤,因爲它與文檔相矛盾。我非常確定''root'應該如何工作。 – Zenexer

回答

0

好的,第二次讀後咖啡。從包含的fastcgi配置中獲取SCRIPT_FILENAME並將其設置在兩個位置塊中。如果這不能解決問題,那麼在coolapp位置硬編碼doc根路徑,看看是否能解決問題。

+0

感謝您的回答。你能否顯示代碼在'coolapp位置硬編碼doc根路徑'的代碼會是什麼樣子?從聽起來像,你想我把'root/var/www/site/htdocs;'那裏,我不明白這會有什麼幫助,因爲應該繼承我想。 –

+0

在fastcgi_param SCRIPT_FILENAME位中,用'/ var/www/apps/coolapp'替換$ document_root。我懷疑變量是混亂的,硬編碼會排除我們的確認。 – Melvyn

+0

啊,非常好。我認爲我越來越接近了......我已將該行更改爲'/ var/www/apps/coolapp $ fastcgi_script_name;'這兩個位置塊都不需要,只需要一個匹配的php文件,對吧?如果是這樣,在fcgi.conf中應該沒問題。但我仍然得到相同的結果。我將在後期更新我的代碼,並添加nginx錯誤消息。 –

1

這似乎已經修復一個新版本。只要SCRIPT_FILENAME設置爲$request_filename,它應該按預期工作。 (這與以前版本不同,其中$request_filename在任何情況下都不起作用。)另外,您需要省略內部位置塊中的任何try_files指令。重新評估$uri似乎甩開$request_filename