带别名的嵌套位置

时间:2014-02-27 20:01:11

标签: configuration nginx location alias

我有一个目录/网络应用程序,位于我网站的网站根目录之外。

说网站在这里:

/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服务器。

我在主alias文件包含的production.conf中添加了nginx.conf指令。这适用于除.php文件之外的所有内容,因为还有另一个location正在捕获.php文件。所以我用location嵌套alias来捕获.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"  

有谁看到我做错了什么?

2 个答案:

答案 0 :(得分:1)

这似乎已在较新版本中修复。只要SCRIPT_FILENAME设置为$request_filename,它就应该按预期工作。 (这与以前的版本不同,$request_filename在所有情况下都不起作用。)此外,您需要在内部位置块中省略任何try_files指令。重新评估$uri似乎会放弃$request_filename

答案 1 :(得分:0)

好的,咖啡后第二次读。从包含的fastcgi配置中获取SCRIPT_FILENAME并将其设置在两个位置块中。如果这不能解决问题,那么在coolapp位置硬编码doc根路径,看看是否能解决问题。