【问题标题】:Nginx setup for laravel project, which has routes with ".php" endings用于 laravel 项目的 Nginx 设置,其路由以“.php”结尾
【发布时间】:2019-01-11 07:18:42
【问题描述】:

我正在尝试使用 Nginx 设置我的 Laravel 项目。我的/etc/nginx/conf.d/default.conf 是:

server {
    listen 80;
    index index.php index.html;
    root /var/www/public;
    location / {
        try_files $uri /index.php?$args;
    }
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

我对带有“.php”结尾的路由有疑问,例如

Route::get('modules.php', 'ModuleController@index');

服务器尝试打开不存在的文件modules.php,而不是去index.php 并在那里查看路由。 我知道,nginx设置中的那个问题,但我没有经验,所以我无法自己解决。

【问题讨论】:

    标签: php nginx laravel-5


    【解决方案1】:

    浏览此页面https://www.digitalocean.com/community/tutorials/how-to-install-laravel-with-an-nginx-web-server-on-ubuntu-14-04

    在这里您可以找到合适的配置和描述。 例如:

    server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;
    
        root /var/www/laravel/public;
        index index.php index.html index.htm;
    
        server_name server_domain_or_IP;
    
        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }
    
        location ~ \.php$ {
            try_files $uri /index.php =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass app:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    }
    

    【讨论】:

      【解决方案2】:

      try_files 语句添加到第二个location 块。

      例如:

      location / {
          try_files $uri /index.php?$args;
      }
      location ~ \.php$ {
          try_files $uri /index.php?$args;
      
          fastcgi_pass app:9000;
          include fastcgi_params;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      }
      

      【讨论】:

      • 谢谢。此解决方案与第一个解决方案一样有效,但我将第一个解决方案标记为正确,只是因为它是较早发送的。
      猜你喜欢
      • 2016-07-27
      • 1970-01-01
      • 2016-12-08
      • 2022-01-02
      • 1970-01-01
      • 1970-01-01
      • 2021-04-13
      • 2017-02-15
      • 2017-06-11
      相关资源
      最近更新 更多