7

我刚刚设置了 Nginx,并试图用它来托管一个 Laravel 应用程序,但我遇到了 2 个问题。

  1. 对于 GET 方法,我总是在输入中得到一个额外的参数。
    • 使用 PostMan (Chrome) 进行测试,设置目标 URL 和所需参数并发送请求。我得到的输出,它总是包含REQUEST_URI它不应该包含的输出。示例输出:

.

Array (
  [/api/user] => // This shouldn't be here
  [test] => test
)
  1. 我的参数(以上)根本不会显示 DELETE 或 PUT,而对于 POST,我只会得到REQUEST_URI

Nginx 虚拟主机(随后使用 Nginx设置 Laravel

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只有一件事发生了变化,那就是keepalive_timeout从 65 到 15

所以我完全不知道这一切哪里出了问题。但我不得不提一下,在我拥有的另外 2 个环境(一个使用 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;
}

这将使 GET 工作......并添加附加参数

4

5 回答 5

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;
}

至于意外输入,可能是您当前重写的工作方式,但可以肯定地说,您在输出什么?

于 2013-02-13T13:15:46.263 回答
2

这对我有用:

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;

}
于 2013-02-24T09:39:00.880 回答
1

从您的配置:

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

在这里你有一个重定向到/index.php?/$1(例如/index.php?/some/path)。

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

^(.+\.php)(/.+)$在这里,您通过正则表达式(例如/index.php/some/path)溢出了路径。

你注意到区别了吗?

于 2013-02-18T17:13:20.373 回答
1

我遇到了类似的问题,我用以下配置修复了它:

server {
    listen 80;
    server_name subdomain.domain.com;
    root /var/www/dir/public;

    charset utf-8;

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log  /var/log/nginx/registration.app-error.log error;
    error_page 404 /index.php;
    sendfile off;

    # Point index to the Laravel front controller.
    index index.php;

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

    location ~ \.php$ {
                include snippets/fastcgi-php.conf;
        #
        #       # With php7.0-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000;
        #       # With php7.0-fpm:
                fastcgi_pass unix:/run/php/php7.2-fpm.sock;
    }



    location ~ /\.ht {
        #deny all;
    }
}
于 2018-06-21T01:15:01.387 回答
0

This is a configuration that works for me with NGINX and 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;
    }

}
于 2014-01-13T15:18:37.380 回答