使用NginX和Laravel:URL重写 [英] Using NginX and Laravel: URL Rewrites

查看:96
本文介绍了使用NginX和Laravel:URL重写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在运行CentOS 6.4和NginX 1.8的VPS上设置Laravel框架.除了使清洁的URL不能正常工作之外,我可以使所有其他功能都能正常工作,例如使用"website.com/home"而不是"website.com/index.php/home".有人可以帮忙吗?这是当前我的虚拟主机配置文件的内容.

I'm trying to setup the Laravel framework on my VPS running CentOS 6.4 and NginX 1.8. I can get everything else to work perfectly, except I can't get the cleaner URLs to work, like using "website.com/home" instead of "website.com/index.php/home". Can anyone help? This is the contents of my virtual host configuration file currently.

server {

    listen 80;
    server_name swati.havok.semicolony.com;
    access_log /usr/share/nginx/semicolony.com/_subdomains/swati/storage/logs/access.log;
    error_log /usr/share/nginx/semicolony.com/_subdomains/swati/storage/logs/errors.log;

    root /usr/share/nginx/semicolony.com/_subdomains/swati/public;
    index index.php;

    #browse folders if no index file
    autoindex on;

    # enforce NO www
    if ($host ~* ^www\.(.*))
    {
        set $host_without_www $1;
        rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent;
    }

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

    # removes trailing "index" from all controllers
    if ($request_uri ~* index/?$)
    {
        rewrite ^/(.*)/index/?$ /$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;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }

    location ~ \.php$ {
    root                    /usr/share/nginx/semicolony.com/_subdomains/swati/public;
    fastcgi_pass            127.0.0.1:9000;
    fastcgi_index           index.php;
    fastcgi_param SCRIPT_FILENAME   $document_root$fastcgi_script_name;
    include             fastcgi_params;
    }

    location ~ /\.ht {
        deny  all;
    }
}

使用Laravel 4可能很重要.

It may be of importance that I am using Laravel 4.

推荐答案

我对Nginx不太熟悉,但我确实认为您进行重写的正则表达式不正确.

I am not very familiar with Nginx, but I do think that your regex for the rewrite is incorrect.

尝试更改

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

收件人:

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

或者:

rewrite ^.*$ /index.php last;

这篇关于使用NginX和Laravel:URL重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆