Laravel 4和Nginx中的奇怪路由问题

时间:2015-02-20 17:00:14

标签: php laravel nginx laravel-4 routing

我对Laravel 4和Nginx有一个奇怪的路由问题。我相信这很可能是一个Nginx问题,而不是Laravel。

可以访问任何存在的路径:

/index.php/route

更奇怪的是,index.php之前的任何内容也都有效:

/abcdefg/abcdef/abcde/abcd/abc/ab/a/index.php/route

路由的长度并不重要,只要在routes.php中正确定义了index.php之后的任何段。

第一个示例使用CSS / JS等加载正确的路由,但第二个示例没有,只加载HTML。

有没有人遇到过这种情况,如果是这样的话,你怎么能阻止这种情况发生呢?感谢。

更新:好的人,下面是我的Nginx配置:

# Enable compression both for HTTP/1.0 and HTTP/1.1.
gzip_http_version 1.1;

# Compression level (1-9).
# 5 is a perfect compromise between size and cpu usage, offering about
# 75% reduction for most ascii files (almost identical to level 9).
gzip_comp_level 5;

# Don't compress anything that's already small and unlikely to shrink much
# if at all (the default is 20 bytes, which is bad as that usually leads to
# larger files after gzipping).
gzip_min_length 500;

# Compress data even for clients that are connecting to us via proxies,
# identified by the "Via" header (required for CloudFront).
gzip_proxied any;

# Tell proxies to cache both the gzipped and regular version of a resource
# whenever the client's Accept-Encoding capabilities header varies;
# Avoids the issue where a non-gzip capable client (which is extremely rare
# today) would display gibberish if their proxy gave them the gzipped version.
gzip_vary on;

# Compress all output labeled with one of the following MIME-types.
gzip_types application/atom+xml application/x-javascript text/javascript application/json application/rss+xml application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/svg+xml image/x-icon text/css text/plain text/x-component;

server {
    listen 80;
    server_name www.domain.com domain.com;
    return 301 https://domain.com$request_uri;
}

server {
    listen 443 ssl;
    server_name domain.com;
    root /home/forge/default/public;

    if ($host = 'www.domain.com') {
        rewrite ^/(.*)$ https://domain.com/$1 permanent;
    }

    # FORGE SSL (DO NOT REMOVE!)
    # ssl on;
    ssl_certificate /etc/nginx/ssl/default/4980/server.crt;
    ssl_certificate_key /etc/nginx/ssl/default/4980/server.key;

    index index.html index.htm index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
        # auth_basic            "Restricted Area";
        # auth_basic_user_file  /home/forge/default/.htpasswd;
    }

    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/default-error.log error;

    error_page 404 /index.php;
    # error_page 404 https://domain.com/404;

    location ~ \.php$ {
        try_files $uri $uri/ /index.php?$query_string;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

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

    location ~ /\.ht {
        deny all;
    }

    # Expire rules for static content

    # cache.appcache, your document html and data
    location ~* \.(?:manifest|appcache|html?|xml|json)$ {
      expires -1;
      # access_log logs/static.log; # I don't usually include a static log
    }

    # Feed
    location ~* \.(?:rss|atom)$ {
      expires 1h;
      add_header Cache-Control "public";
    }

    # Media: images, icons, video, audio, HTC
    location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc|woff)$ {
      expires 1M;
      access_log off;
      add_header Cache-Control "public";
    }

    # CSS and Javascript
    location ~* \.(?:css|js)$ {
      expires 1y;
      access_log off;
      add_header Cache-Control "public";
    }

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
}

2 个答案:

答案 0 :(得分:0)

尝试将第二个位置块更改为:

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

答案 1 :(得分:0)

好的人,看起来我已经解决了这个问题。我最初尝试使用Route :: filter来检测index.php,然后执行重定向,但是没有用,所以我之前使用过App ::所以:

App::before(function($request)
{
    if (strpos($request->url(), 'index.php/'))
    {
        return Redirect::to(str_replace(['index.php/'], '', $request->url()), 301);
    }

    if (strpos($request->url(), 'index.php'))
    {
        return Redirect::to(str_replace(['index.php'], '', $request->url()), 301);
    }
});

这解决了这两个问题,并将请求重定向到不再包含index.php的URI。希望这有助于其他可能遇到这个问题的人。