Laravel Nginx 配置

    之前本地一直使用的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 就能同時訪問對個項目,所以纔會遇到這個問題。

 

成長~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章