【问题标题】:Nginx rewrite argumentsNginx 重写参数
【发布时间】:2015-07-08 18:43:35
【问题描述】:

我将 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 错误。正如我所说,我还没有得到重写。需要一些帮助才能开始。

提前致谢。

【问题讨论】:

    标签: php mod-rewrite nginx request-uri


    【解决方案1】:

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

    location / {                                                            
     index  index.html index.htm index.php;
     if (!-d $request_filename) {
      rewrite  ^/(.*)$ /index.php?url=$1  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;
    }
    

    【讨论】:

    • 这就像我所看到的魅力。我几乎,但几乎,之前有同样的重写,但我没有if(!-d ...) 声明。谢谢!这解决了它。 .git 指令是干什么用的?
    • .git 仅在您使用 git 部署时才有用,否则将其删除。我有这个是因为我从沙箱服务器复制了该配置,我只是从我的 github 存储库中提取代码
    【解决方案2】:

    试试这个:

    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=$1 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;
    }
    
    }
    

    【讨论】:

    • 我也测试了这个,但是当我使用这个时,我的资源(CSS、JS、IMG)由于某种原因没有加载。但我得到了它与@Irossy 发布的代码一起工作。不过谢谢。
    猜你喜欢
    • 2018-10-29
    • 1970-01-01
    • 1970-01-01
    • 2016-09-29
    • 2015-08-26
    • 1970-01-01
    • 1970-01-01
    • 2014-02-18
    • 1970-01-01
    相关资源
    最近更新 更多