Laravel Nginx 配置

在从Apache切换到MxSrvs的Nginx环境后,作者遇到了Laravel路由404的问题。尝试修改location ~ .php$未成功,后发现需将Nginx的root路径指向项目的public目录,并结合try_files指令,解决了访问路由需带index.php的困扰。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    之前本地一直使用的apache+Laravel的配置,这次换了一个集成环境 MxSrvs ,里面使用的是nginx服务,默认的nginx配置档如下:


user user staff;
worker_processes  1;

error_log  /Applications/MxSrvs/logs/errors_nginx.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    client_max_body_size 20m;
    gzip  on;
    gzip_http_version 1.0;
    gzip_disable "MSIE [1-6].";
    gzip_types text/plain application/x-javascript text/css text/javascript;

    include virtualhost.conf;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name 127.0.0.1 localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /Applications/MxSrvs/www;
            index  index.html index.htm index.php;
        }
        
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        
        location ~ \.php$ {
            root           /Applications/MxSrvs/www;
            fastcgi_pass   127.0.0.1:10080;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

    #省略很多已注释的信息

}

Laravel项目加入后,可以访问index.php目录,但配置的其他路由均显示为404.

    在网上找了各种很多文章,更改方式为将 location ~ \.php$ 更改为 location ~ \.php(.*)$,但并不能很好的解决问题,虽然能访问新配置的路由,但访问方式必须是这种:http://localhost/xx/public/index.php/test,index.php必须加上,说实话,不是很友好。

恢复成默认的nginx档,然后继续找解决办法,在网上看到Laravel讲的Nginx配置只需要加上

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

可是我试了,仍然不能解决问题。。。。。但是发现了一个问题,当加上这句,访问 http://localhost/xx/public/test会跳转到集成环境的主页,不加上这句,会报404, 好像有点灵感了。将nginx中root路径指向项目的public目录,即如下:

 

这样就可以正常访问新增的路由了。Laravel 讲述的 try_files $uri $uri/ /index.php?$query_string; 也是要加上的。

这个问题并不是所有人都会遇到,因为个人习惯问题,我喜欢将root路径设置为集成环境中对应文档放置目录,只要加上  项目名/public 就能同时访问对个项目,所以才会遇到这个问题。

 

成长~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值