【问题标题】:Nested location with alias具有别名的嵌套位置
【发布时间】:2014-03-31 12:10:23
【问题描述】:

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

假设网站在这里:

/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 文件。所以我在alias 中嵌套了一个location 来捕获.php 文件,但是现在nginx 告诉我它找不到.php 文件“404 文件未找到”。这是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"  

有人看到我做错了吗?

【问题讨论】:

  • 我们看不出有什么问题,因为它很可能在 mage_rewrites.conf 中。
  • 经过一些测试,当使用别名指令时,$uri 似乎没有更新:$uri 仍将以/coolapp/ 为前缀。这可能是一个错误,因为它与文档相矛盾。我很确定root 应该是这样工作的。

标签: configuration nginx location alias


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 感谢您的回答。你能展示一下in the coolapp location hardcode the doc root path 的代码是什么样的吗?听起来,你想让我把 root /var/www/site/htdocs; 放在那里,我看不出这有什么帮助,因为我认为应该继承。
    • 在 fastcgi_param SCRIPT_FILENAME 位中,将 $document_root 替换为 /var/www/apps/coolapp。我怀疑变量是杂乱无章的,硬编码会排除我们的确认。
    • 啊,很好。我想我越来越近了。我已将该行更改为/var/www/apps/coolapp$fastcgi_script_name; 这两个位置块都不需要,只需一个匹配的php文件,对吗?如果是这样,在 fcgi.conf 中应该没问题。但我仍然得到相同的结果。我将在帖子中更新我的代码并添加 nginx 错误消息。
    • 进行这些更改时是否需要重新启动php-fpm?并不是说它有帮助,只是想知道我是否需要继续这样做,因为如果没有必要,我宁愿不必这样做:)
    • 啊,我明白了!需要用/var/www/apps 替换$document_root!谢谢,你引导我回答:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-09
    • 2017-12-11
    • 2016-06-21
    • 2021-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多