繁体   English   中英

如何在Nginx中使用重写?

[英]How to use rewrite in Nginx?

我在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,你能帮我解决这个错误吗? 谢谢。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM