Nginx 重写参数

Nginx rewrite arguments

我将 Nginx 与我的网络服务器一起使用,但不完全了解重写在这里的工作原理。我让它在 Apache 上运行。

我使用基于 MVC 的 PHP 项目,我想像使用 Apache 一样使用干净的 URL。

www.domain.com/index.php?url=home/index 工作正常。 www.domain.com/home/index 不起作用。 如您所见,我想要像上一个一样干净 URL。

我试过几次改写,不知道该用try_files还是改写。但我猜它是在重写我之后。

正如我所说,我仍在学习重写。 我的服务器块如下所示:

server {

listen 80 default_server;
listen [::]:80 default_server ipv6only=on;

root /usr/share/nginx/html/pub/;
index index.php index.html index.htm;

server_name localhost;

location / {
    try_files $uri $uri/ @rewrite;
}

location @rewrite {
    rewrite ^/index.php?$arg_url /$arg_url permanent;
}

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}

}

我认为我的重写块有意义,但我想它没有。尝试了几次重写,我得到 500 或 404 错误。正如我所说,我还没有得到重写。需要一些帮助才能开始。

提前致谢。

也不是 nginx 专家,但我认为您正在寻找这样的东西:

location / {                                                            
 index  index.html index.htm index.php;
 if (!-d $request_filename) {
  rewrite  ^/(.*)$ /index.php?url=  last;
  break;                                                  
 }
}   

然后你可以删除那个重写指令

我的其余配置如下所示:

location ~ \.php$ {
 root /var/www/path;
 fastcgi_split_path_info ^(.+\.php)(/.+)$;
 #try_files $uri=404;
 fastcgi_pass unix:/var/run/php5-fpm.sock;
 fastcgi_index index.php;
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name

 include fastcgi_params;
 fastcgi_buffer_size 128k;
 fastcgi_buffers 2564k;
 fastcgi_busy_buffers_size 256k;
 fastcgi_temp_file_write_size 256k;

}
location ~ /\.git {
 denyall;
}

#Staticfileslocation

location ~* ^.+.(swf|jpg|jpeg|gif|png|ico|css|bmp|js|zip)$ {
 expires max;
 root /var/www/path;
}

试试这个:

server {

listen 80 default_server;
listen [::]:80 default_server ipv6only=on;

root /usr/share/nginx/html/pub/;
index index.php index.html index.htm;

server_name localhost;

location / {
    try_files $uri $uri/ @rewrite;
}

location @rewrite {
    rewrite ^/(.*)$ /index.php?url= last;
}

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}

}