【问题标题】:How to properly configure alias directive in nginx?如何在 nginx 中正确配置别名指令?
【发布时间】:2015-04-13 22:50:02
【问题描述】:

我一直在尝试在我的 nginx 网络服务器上配置多个 webapp,但我无法运行一个需要将 $document_root 设置为 laravel 公共文件夹的 Laravel 应用程序。 我目前正在尝试使用别名指令对其进行配置,但由于不明原因,这不起作用。这就是我想要做的。

# Default server configuration
#
server {
    listen 80;

    # SSL configuration
    #
    listen 443 ssl;

    error_log /var/log/nginx/error.log warn;

    ssl_certificate /etc/nginx/ssl/server.crt;
    ssl_certificate_key /etc/nginx/ssl/server.key;


    set $root_path '/var/www/html';
    root $root_path;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html index.php;

    server_name localhost;

    location /paperwork {
        alias /var/www/html/paperwork/frontend/public;
        try_files $uri $uri/;
        #location ~ \.php {
        #   fastcgi_split_path_info ^(.+\.php)(.*)$;
        #   fastcgi_pass unix:/var/run/php5-fpm.sock;
        #   include /etc/nginx/fastcgi_params;
        #   #fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
        #   #fastcgi_intercept_errors on;
        #}
    }

    #location @paperwork {
    #   rewrite /paperwork/(.*)$ /paperwork/index.php/$1 last;
    #}

    location / {

    }


    location /wallabag {
        try_files $uri $uri/ /index.php;
    }

    location /laverna {
        try_files $uri/ /index.php;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        # With php5-cgi alone:
        #fastcgi_pass 127.0.0.1:9000;
        # With php5-fpm:
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        #try_files $uri $uri/ =404;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }



    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one

    location ~ /\.ht {
        deny all;
    }
}

为了测试我的“别名”配置,我在 /var/www/html/paperwork/frontend/public/test.php 中放置了一个“test.php”文件,并尝试通过https://IP/paperwork/test.php 访问它。我收到 404 错误,而 nginx 错误日志中没有任何内容。 如果我在浏览器中尝试https://IP/paperwork/frontend/public/test.php,它会显示 test.php 文件而没有错误。 如果我在 php 位置取消注释 try_files 行,没有任何变化。

如果我将 test.php 复制到 /var/www/html/paperwork/test2.php 并访问 https://IP/paperwork/test2.php 文件显示没有错误,所以我可以在这里看到别名不起作用,因为没有 test2 .php 在文书工作公共目录中。

如果我取消注释文件位置内的 php 位置,我可以有不同的行为。这样,https://IP/paperwork/test.php 之类的请求不会显示 404,而是显示空白屏幕。

我已经浏览了很多与此相关的论坛/问题,但我无法为显示 test.php 之类的简单任务获得工作配置...

谢谢!

【问题讨论】:

  • 感谢您提出如此有用的问题.. 先生,您让我开心

标签: php nginx alias


【解决方案1】:

我找到了解决方案。似乎为 php 文件发送了错误的请求。使用别名时,建议使用 $request_filename 而不是 $fastcgi_script_name。

这是我的位置块:

location /paperwork {

      alias /var/www/html/paperwork/frontend/public;
      #try_files $uri $uri/;
      location ~ \.php$ {
          fastcgi_pass unix:/var/run/php5-fpm.sock;
          include fastcgi_params;                       
          fastcgi_param SCRIPT_FILENAME $request_filename;
          #fastcgi_intercept_errors on;
      }
}

这解决了我的“test.php”文件的问题,该文件现在在到达https://IP/paperwork/test.php 时执行。所以 alias 正在工作并且 php 执行得很好。 尝试访问“index.php”(这是我的 laravel 应用程序索引)时,我仍然遇到问题。找到了文件,但下载了它而不是执行它。因此,当我到达https://IP/paperwork/index.php 时,我下载了一个登录文件,即 index.php 文件。如果我尝试 /paperwork/index.php/login 或 /paperwork/login,我会得到相同的行为。

【讨论】:

  • 是的!谢谢。关键是使用$request_filename。其他所有示例都有fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name,但这不起作用。
  • 我遇到了同样的问题,无法执行 index.php 文件。我也尝试在我的位置设置索引 index.php 但没有运气。
  • 指令alias /some/path; + fastcgi_param SCRIPT_FILENAME $request_filename; 需要指令index index.php;在位置级别与index.php 一起使用
  • 我可以吻你。
  • 似乎没有办法让这个(或其他任何东西)与index 文件一起工作。添加index index.php; 只会下载文件。
【解决方案2】:

试试这个:

location /api/ {
     index  index.php index.html index.htm;
     alias /app/www/;
     location ~* "\.php$" {
         try_files      $uri =404;
         fastcgi_pass   127.0.0.1:9000;
         fastcgi_param  SCRIPT_FILENAME  $request_filename;
     }
 }

https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/

【讨论】:

    猜你喜欢
    • 2012-04-22
    • 2020-10-17
    • 2013-12-23
    • 2014-02-15
    • 1970-01-01
    • 2014-03-20
    • 1970-01-01
    • 2021-02-18
    • 2017-10-01
    相关资源
    最近更新 更多