如何在nginx中正确配置别名指令?

时间:2015-02-13 00:17:21

标签: php nginx alias

我一直在尝试在我的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访问它。我在nginx错误日志中得到404错误而没有任何内容。 如果我在浏览器中尝试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这样的简单任务获得工作配置......

谢谢!

1 个答案:

答案 0 :(得分:30)

我找到了解决方案。似乎为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时执行。所以别名正在运行,php执行得很好。 我试图达到'index.php'(这是我的laravel应用程序索引)时仍有问题。找到文件,但不下载执行文件。所以当我到达https://IP/paperwork/index.php时,我得到一个下载的登录文件,它是index.php文件。如果我尝试/paperwork/index.php/login或/ paperwork / login,我会得到相同的行为。