2013-02-13 20 views
6

我剛剛設置了Nginx,我試圖用它來託管一個Laravel應用程序,但我遇到了兩個問題。帶Nginx參數的Laravel是空的

  1. 對於GET方法,我總是會在我的輸入中獲得一個額外的參數。
    • 使用PostMan(Chrome)進行測試,我設置了目標URL和我想要的參數併發送請求。我得到的輸出總是包含REQUEST_URI,它不應該。示例輸出:

Array (
    [/api/user] => // This shouldn't be here 
    [test] => test 
) 
  • 我的參數(以上)的DELETE將節目或PUT,在所有的,以及POST我只得到REQUEST_URI
  • Nginx的虛擬主機(隨後Setting up Laravel w/ Nginx

    server { 
        server_name local.test.com; 
        root /var/www/test/public; 
    
        location/{ 
         index index.php index.html index.htm; 
        } 
    
        # serve static files directly 
        location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ { 
         access_log off; 
         expires max; 
        } 
    
        # removes trailing slashes (prevents SEO duplicate content issues) 
        if (!-d $request_filename) { 
         rewrite ^/(.+)/$ /$1 permanent; 
        } 
    
        # unless the request is for a valid file (image, js, css, etc.), send to bootstrap 
        if (!-e $request_filename) { 
         rewrite ^/(.*)$ /index.php?/$1 last; 
         break; 
        } 
    
        # catch all 
        error_page 404 /index.php; 
    
        # The PHP Inclusion Block 
        # include /etc/nginx/includes/php; 
        location ~ \..*/.*\.php$ { 
         # I'm pretty sure this stops people trying to traverse your site to get to other PHP files 
         return 403; 
        } 
    
        #location ~ \.php$ { 
        location ~ \.php(.*)$ { 
         try_files $uri =404; 
         fastcgi_split_path_info ^(.+\.php)(/.+)$; 
         fastcgi_pass 127.0.0.1:9000; 
         fastcgi_index index.php; 
         include /etc/nginx/fastcgi_params; 
        } 
    
    # Deny Any Access to .htaccess Files That May Be Present (not usually in issue in Laravel) 
    # include /etc/nginx/includes/deny_htaccess; 
    location ~ /\.ht 
    { 
        deny all; 
    } 
    
        error_log /var/www/logs/test-error.log; 
    } 
    

    fastcgi_params

    fastcgi_param QUERY_STRING   $query_string; 
    fastcgi_param REQUEST_METHOD   $request_method; 
    fastcgi_param CONTENT_TYPE   $content_type; 
    fastcgi_param CONTENT_LENGTH   $content_length; 
    
    fastcgi_param SCRIPT_FILENAME   $request_filename; 
    fastcgi_param SCRIPT_NAME    $fastcgi_script_name; 
    fastcgi_param REQUEST_URI    $request_uri; 
    fastcgi_param DOCUMENT_URI   $document_uri; 
    fastcgi_param DOCUMENT_ROOT   $document_root; 
    fastcgi_param SERVER_PROTOCOL   $server_protocol; 
    
    fastcgi_param GATEWAY_INTERFACE  CGI/1.1; 
    fastcgi_param SERVER_SOFTWARE   nginx/$nginx_version; 
    
    fastcgi_param REMOTE_ADDR    $remote_addr; 
    fastcgi_param REMOTE_PORT    $remote_port; 
    fastcgi_param SERVER_ADDR    $server_addr; 
    fastcgi_param SERVER_PORT    $server_port; 
    fastcgi_param SERVER_NAME    $server_name; 
    
    #fastcgi_param HTTPS     $https; 
    
    # PHP only, required if PHP was built with --enable-force-cgi-redirect 
    fastcgi_param REDIRECT_STATUS   200; 
    
    fastcgi_connect_timeout     60; 
    fastcgi_send_timeout     180; 
    fastcgi_read_timeout     180; 
    fastcgi_buffer_size      128k; 
    fastcgi_buffers 4      256k; 
    fastcgi_busy_buffers_size    256k; 
    fastcgi_temp_file_write_size   256k; 
    fastcgi_intercept_errors    on; 
    

    nginx.conf只有1個的東西改變,那就是從65 keepalive_timeout至15

    所以我絕對不知道,所有的這件事情不順心。但我不得不提一提,在另外兩個環境中(一個使用Lighttpd,另一個使用Apache2),該應用程序完美無缺。

    從我注意到,它的所有歸納爲以下代碼:

    # unless the request is for a valid file (image, js, css, etc.), send to bootstrap 
    if (!-e $request_filename) { 
        rewrite ^/(.*)$ /index.php?/$1 last; 
        break; 
    } 
    

    這將使找份工作......並添加附加參數

    回答

    5

    最好是避免不必要的重寫你的nginx的配置(見Nginx Pitfalls),其中特別是一個負責傳遞請求到Laravel前端控制器:

    所有你需要的是Laravel:

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

    首先嚐試直接訪問文件,然後是目錄,如果不存在,則將請求傳遞給index.php。 $query_string是重要的傳遞,因爲這將包含$_GET數據,否則將丟失。

    這裏是我自己的FastCGI配置一塊:

    location ~ \.php$ { 
        fastcgi_pass 127.0.0.1:9000; 
        fastcgi_index index.php; 
        fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name; 
        include  fastcgi_params; 
    } 
    

    至於意外的輸入,也可能是你的方式重寫當前工作,但是可以肯定地說,你有什麼輸出?

    +0

    'DD對我的作品的配置(輸入::所有());'這就是我」 m輸出...並且不是,它不是'Input :: all()',它設置了不需要的參數 – Alex 2013-02-13 13:18:40

    +0

    好的。嘗試建議的配置,並告訴我是否它。 – Gargron 2013-02-13 13:23:45

    +0

    剛剛嘗試過,通過評論包含重寫規則的「if」,現在我根本沒有輸出我的參數 – Alex 2013-02-13 13:24:45

    1

    你的配置:

    rewrite ^/(.*)$ /index.php?/$1 last; 
    

    在這裏你有一個重定向到/index.php?/$1(例如​​)。

    fastcgi_split_path_info ^(.+\.php)(/.+)$; 
    

    ,在這裏您可以通過^(.+\.php)(/.+)$正則表達式(例如/index.php/some/path)溢出的路徑。

    你有沒有注意到區別?

    +0

    我實際上刪除了'fastcgi_spli'部分,現在我得到了參數,但是我仍然有'[/ api/user] =>//這不應該在這裏問題 – Alex 2013-02-22 14:23:08

    +0

    我遵循http://stackoverflow.com/questions/8856664/setting-up-laravel-w-nginx,只是addapted有點 – Alex 2013-02-22 14:23:47

    +0

    但你已經配置nginx這種方式。你'重寫^ /(。*)$ /index.php?/$1 last;'負責將參數添加到路徑中。 – VBart 2013-02-22 15:14:02

    1

    這個工作對我來說:

    location/{ 
        index index.php; 
        try_files $uri $uri/ /index.php?q=$uri&$args; 
    } 
    
    location ~ \.php$ { 
    
        include  fastcgi_params; 
        fastcgi_pass 127.0.0.1:9000; 
        fastcgi_index index.php; 
    
        fastcgi_split_path_info     ^(.+\.php)(/.+)$; 
        fastcgi_param PATH_INFO     $fastcgi_path_info; 
        fastcgi_param PATH_TRANSLATED   $document_root$fastcgi_path_info; 
        fastcgi_param SCRIPT_FILENAME   $document_root$fastcgi_script_name; 
    
    } 
    
    +0

    我不認爲Laravel期待'q'參數,以前沒有看過。但看到它在CI – Alex 2013-02-26 10:03:42

    +0

    是的,我已經測試它沒有'q',它工作正常。所以'try_files $ uri $ uri//index.php?$ args;'工作。 – 2013-03-09 07:00:11

    0

    這是與NGINX和Laravel

    server { 
    
        listen 80; 
        server_name sub.domain.com; 
        set $root_path '/var/www/html/application_name/public'; 
        root $root_path; 
    
        index index.php index.html index.htm; 
    
        try_files $uri $uri/ @rewrite; 
    
        location @rewrite { 
         rewrite ^/(.*)$ /index.php?_url=/$1; 
        } 
    
        location ~ \.php { 
    
         fastcgi_pass 127.0.0.1:9000; 
         fastcgi_index /index.php; 
    
         include /etc/nginx/fastcgi_params; 
    
         fastcgi_split_path_info  ^(.+\.php)(/.+)$; 
         fastcgi_param PATH_INFO  $fastcgi_path_info; 
         fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; 
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
        } 
    
        location ~* ^/(css|img|js|flv|swf|download)/(.+)$ { 
         root $root_path; 
        } 
    
        location ~ /\.ht { 
         deny all; 
        } 
    
    }