Nginx 一些常用的 URL 重写方法

红薯 发布于 2010/01/21 21:27
阅读 6K+
收藏 6

【直播预告】破局数据孤岛与采集乱象:Flashcat 为大模型增强可观测性奠定基础

1. 在 Apache 的写法

RewriteCond  %{HTTP_HOST}  nginx.org
RewriteRule (.*) http://www.nginx.org$1

在 Nginx 可以对应写成:

server {
listen 80;
server_name www.nginx.org nginx.org;
if ($http_host = nginx.org) {
rewrite (.*) http://www.nginx.org$1;
}
...
}

但 Nginx 作者更建议的方法是:

server {
listen 80;
server_name nginx.org;
rewrite ^ http://www.nginx.org$request_uri?;
}

server {
listen 80;
server_name www.nginx.org;
...
}

2. 在 Mongrel的写法:

DocumentRoot /var/www/myapp.com/current/public

RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
RewriteRule ^.*$ %{DOCUMENT_ROOT}/system/maintenance.html [L]

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)$ $1 [QSA,L]

RewriteCond %{REQUEST_FILENAME}/index.html -f
RewriteRule ^(.*)$ $1/index.html [QSA,L]

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1/index.html [QSA,L]

RewriteRule ^/(.*)$ balancer://mongrel_cluster%{REQUEST_URI} [P,QSA,L]

在 Nginx可对应写成

location / {
root /var/www/myapp.com/current/public;

try_files /system/maintenance.html
$uri $uri/index.html $uri.html
@mongrel;
}

location @mongrel {
proxy_pass http://mongrel;
}

这个就比Apache 的简化多了。

加载中
0
wankaiming
wankaiming
非常感谢红薯老大呀。。。。。。。。。。。。。。。。。。。
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部