如何在Nginx中正确配置别名指令? [英] How to properly configure alias directive in nginx?

查看:310
本文介绍了如何在Nginx中正确配置别名指令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在Nginx Web服务器上配置多个Web应用程序,但是我无法使用需要将$ document_root设置为laravel公共文件夹的Laravel应用程序. 我目前正在尝试使用别名指令配置它,但是由于一个晦涩的原因,这不起作用.这就是我想要做的.

I have been trying to configure multiple webapp on my nginx webserver but I can't get working one Laravel app that requires $document_root set to laravel public folder. I am currently trying to configure it using alias directive but for an obscure reason this doesn't work. Here is what I am trying to do.

# 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;
    }
}

要测试我的别名"配置,我将"test.php"文件放在/var/www/html/paperwork/frontend/public/test.php中,并尝试通过https://IP/paperwork/test.php .我收到404错误,nginx错误日志中什么也没有. 如果我在浏览器中尝试 https://IP/paperwork/frontend/public/test.php ,则会显示测试.php文件没有错误. 如果我取消对php位置中的try_files行的注释,则没有任何变化.

To test my "alias" config I put a 'test.php' files in /var/www/html/paperwork/frontend/public/test.php and tried to access it via https://IP/paperwork/test.php. I get a 404 error and nothing in nginx error log. If I try https://IP/paperwork/frontend/public/test.php in browser it displays the test.php file without errors. Nothing change if I uncomment try_files line in php location.

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

If I copy test.php to /var/www/html/paperwork/test2.php and access to https://IP/paperwork/test2.php the file is displayed without errors so I can see here that alias is not working as there is not a test2.php in paperwork public directory.

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

I can have a different behaviour if I uncomment php location inside paperwork location. With this, requests like https://IP/paperwork/test.php do not display a 404 but a blank screen.

我已经参加过很多与此相关的论坛/问题,但是我无法获得用于执行诸如显示test.php ...的简单任务的有效配置.

I have been through a lot of forums / questions related to this but I couldn't get a working config for a simple task like displaying test.php...

谢谢!

推荐答案

我找到了解决方案.似乎对php文件发送了错误的请求.使用别名时,建议使用$ request_filename代替$ fastcgi_script_name.

I found the solution. It seems that a wrong request was sent for php files. When alias is used it is recommend to use $request_filename instead of $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,则会得到相同的行为.

This solved my problem for my 'test.php' file which is now executed while reaching https://IP/paperwork/test.php. So alias is working and php is well executed. I still have a problem when trying to reach 'index.php' (which is my laravel app index). File is found but instead of executing it is downloaded. So when I reach https://IP/paperwork/index.php I get a login file downloaded which is index.php file. I get same behaviour if I try /paperwork/index.php/login or /paperwork/login.

这篇关于如何在Nginx中正确配置别名指令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆