使用 NginX 和 Laravel:URL 重写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15864771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:41:06  来源:igfitidea点击:

Using NginX and Laravel: URL Rewrites

url-rewritingnginxlaravel

提问by BraydenJW

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.

我正在尝试在运行 CentOS 6.4 和 NginX 1.8 的 VPS 上设置 Laravel 框架。我可以让其他一切都完美地工作,除了我不能让更干净的 URL 工作,比如使用“website.com/home”而不是“website.com/index.php/home”。任何人都可以帮忙吗?这是我当前的虚拟主机配置文件的内容。

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 ;
        rewrite ^/(.*)$ $scheme://$host_without_www/ 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 ^/(.+)/$ / permanent;
    }

    # removes trailing "index" from all controllers
    if ($request_uri ~* index/?$)
    {
        rewrite ^/(.*)/index/?$ / permanent;
    }

    # unless the request is for a valid file (image, js, css, etc.), send to bootstrap
    if (!-e $request_filename)
    {
        rewrite ^/(.*)$ /index.php?/ 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;
    }
}

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

编辑:我使用 Laravel 4 可能很重要。

回答by Mike Rockétt

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

我对 Nginx 不是很熟悉,但我确实认为您重写的正则表达式不正确。

Try changing

尝试改变

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

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

To:

到:

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

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

Or:

或者:

rewrite ^.*$ /index.php last;

rewrite ^.*$ /index.php last;

回答by Fábio N Lima

Here is my nginx / Laravel configuration (Debian in Linode):

这是我的 nginx/Laravel 配置(Linode 中的 Debian):

# Laravel 4 nginx configuration
server {

        server_name www.yourdomain.com;

        access_log /var/www/yourdomain.com/log/access.log;
        error_log /var/www/yourdomain.com/log/error.log;

        root /var/www/yourdomain.com/public;
        index index.php index.html index.htm;

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

        location ~ \.php$ {

                # With php5-fpm:
                include /opt/nginx/conf/fastcgi_params; #Here you need to point your nginx instalation directory
                fastcgi_pass unix:/var/run/php5-fpm.sock; #Use this config if you are using sock or use fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

        }

}