加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > Nginx > 正文

php – 如何在nginx中正确配置alias指令?

发布时间:2020-12-13 21:21:37 所属栏目:Nginx 来源:网络整理
导读:我一直在尝试在我的nginx网络服务器上配置多个webapp但我无法使用一个需要将$document_root设置为laravel公共文件夹的Laravel应用程序. 我目前正在尝试使用别名指令配置它,但由于一个不明确的原因,这不起作用.这是我想要做的. # Default server configuratio

我一直在尝试在我的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文件发送了错误的请求.使用别名时,建议使用$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,我会得到相同的行为.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读