使用 NGINX 重写 URL [英] Rewrite URL with NGINX
问题描述
我有一个用 php 编码的博客,有几篇文章,但要阅读帖子,网址应该类似于 http://myblog/post.php?url=an-article-tilte&id=7代码>.
I have a blog coded with php with several articles, but to read a post the url should be like http://myblog/post.php?url=an-article-tilte&id=7
.
我想用最聪明的方式来访问带有 url 的文章(并改进引用).在我的数据库中,每篇文章都有一个 url
属性,该属性在 'kebab-case'(例如,欢迎来到我的博客"将是欢迎来到我的博客").
I want to make a smartest way to access articles with their url (and to improve the referencing). In my database, each article has an url
attribute that contains its title in 'kebab-case' (eg. 'Welcome to my blog' will be 'welcome-to-my-blog').
总而言之,要访问欢迎来到我的博客"帖子(id 等于 7),我会输入 http://myblog.com/post/welcome-to-my-blog-7
而不是 http://myblog.com/post.php?url=welcome-to-my-blog&id=7
.
To sum up, to access the 'Welcome to my blog' post (with id equal to 7) I would type http://myblog.com/post/welcome-to-my-blog-7
and not http://myblog.com/post.php?url=welcome-to-my-blog&id=7
.
在我的博客 nginx 配置文件中,我有这个:
In my blog nginx configuration file I have this :
server {
listen 80;
root /opt/http/nginx/sites/myblog/www;
index index.php index.html;
server_name myblog.com www.myblog.com;
location / {
try_files $uri $uri/ /index.php;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
}
location ~* \.(pdf)$ {
expires 30d;
}
client_max_body_size 3M;
error_page 403 /index.php;
# pass the PHP scripts to FastCGI server listening on /var/run/php5-fpm.sock
location ~ \.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;
}
gzip on;
gzip_min_length 1100;
gzip_buffers 4 32k;
gzip_types text/plain application/x-javascript text/xml text/css;
gzip_vary on;
access_log /var/log/nginx/www.myblog.access.log;
error_log /var/log/nginx/www.myblog.error.log;
}
而且我想我必须放入这样的东西,但是当我尝试重新启动 nginx 时会出错.所以我不知道它有什么问题.
And I guess I have to put inside something like this, but it makes an error when I try to restart nginx. So I don't know what's wrong in it.
location @rewrites {
if ($uri ~* ^/post/([a-zA-Z0-0\-]+)-([0-9]+)) {
rewrite ^/post.php?url=$1&id=$2;
}
}
使用 apache 很容易,但有没有办法在 nginx 上重现它?
With apache it was easy, but is there a way to reproduce it on nginx?
推荐答案
我认为问题是缺少美元 $
来关闭正则表达式字符串,您无论如何都可以将规则重写如下:>
I think the problem is the missing dollar $
to close the regex string, you can anyhow rewrite your rule as following:
location /post/ {
rewrite ^/post/([\w-]+)-(\d+)$ /post.php?url=$1&id=$2;
}
地点:
\w
等价于[a-zA-Z0-9_]
(如果你喜欢去掉 unescore_
在扩展中重写它版本)\d
是[0-9]
\w
is equivalent to[a-zA-Z0-9_]
(if you prefer remove unescore_
rewrite it in the extended version)\d
is[0-9]
注意:如果你使用~*
,它是一个不区分大小写的匹配(不需要双A-Za-z
).
NOTE: if you use ~*
it's a case insensitive match (the double A-Za-z
is not needed).
更新:如果我正确理解你需要什么,我们必须重写规则来做相反的事情(向 post.php?url={kebab-title}&id={id-number}
并实际获取``post/{kebab-title}-{id-number}
UPDATE: if i understand correctly what do you need, we have to rewrite the rule to do the opposite (make a request to post.php?url={kebab-title}&id={id-number}
and actually obtain the url of ``post/{kebab-title}-{id-number}
location ~ ^/post\.php\?url=(.*)&id=(\d*)$ {
alias /post/$1-$2;
}
更新 2为了避免 /post/
下不需要的匹配,您还可以使用此替代(更具体)版本:
UPDATE 2
to avoid unwanted matches under /post/
you can also use this alternative (more specific) version:
location ~ ^/post/((?:\w+-)+\w+)-(\d+)$ {
rewrite /post.php?url=$1&id=$2;
}
这篇关于使用 NGINX 重写 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!