【问题标题】:How to use rewrite in Nginx?如何在 Nginx 中使用重写?
【发布时间】:2016-12-15 18:49:49
【问题描述】:

我在 Apache2 中使用 .htaccess,效果很好。

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9_]+)$ profile.php?username=$1

现在我使用 nginx 重写,在可用的站点中,我进行了这个配置

server {
listen 80;
server_name domain.com;
root /home/domain;
index index.html index.htm index.php;
location / {
    rewrite "^([A-Za-z0-9_]+)$" profile.php?username=$1 last;
}
location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}
location ~ /\.ht {
    deny all;
}
}

但它不起作用,它显示 404 Not Found,你能帮我解决这个错误吗?谢谢。

【问题讨论】:

    标签: apache .htaccess nginx


    【解决方案1】:

    重写翻译存在问题。在此代码中检查此更新的重写翻译。

    server {
    listen 80;
    server_name domain.com;
    root /home/domain;
    index index.html index.htm index.php;
    location / {
    if (!-e $request_filename){
    rewrite ^/([A-Za-z0-9_]+)$ /profile.php?username=$1;
    }
    }
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }
    location ~ /\.ht {
        deny all;
    }
    }
    

    【讨论】:

    • 非常感谢,你能解释一下为什么这里应该使用if (!-e $request_filename){,在.htaccess中,它使用RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-d
    猜你喜欢
    • 2015-12-20
    • 2011-05-18
    • 2019-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-09
    • 2017-10-15
    • 2017-09-27
    相关资源
    最近更新 更多