重定向到主網站
那些在共享主機期間只使用Apache s .htaccess文件配置所有內容的人,通常會將以下規(guī)則:
#重寫條件
RewriteCond %{HTTP_HOST} example.org
#重寫規(guī)則
RewriteRule (.*) http://www.example.org$1
轉換為如下配置
server {
listen 80;
server_name www.example.org example.org;
if ($http_host = example.org) {
rewrite (.*) http://www.example.org$1;
}
...
}
這是一種錯誤、繁瑣并且無效的方式派哲,正確的方法是為example.org
定義一個單獨的服務器。
server {
listen 80;
server_name example.org;
return 301 http://www.example.org$request_uri;
}
server {
listen 80;
server_name www.example.org;
...
}
考慮另一種情況。如下相味,如果服務器名不是example.com
和www.example.com
就重寫。
RewriteCond %{HTTP_HOST} !example.com
RewriteCond %{HTTP_HOST} !www.example.com
RewriteRule (.*) http://www.example.com$1
只需單獨定義example.com www.example.com
的server和一個默認server即可收毫。
server {
listen 80;
server_name example.com www.example.com;
...
}
server {
listen 80 default_server;
server_name _;
return 301 http://example.com$request_uri;
}
轉換混合規(guī)則
常見的混合規(guī)則
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.html [QSA,L]
RewriteRule ^/(.*)$ balancer://mongrel_cluster%{REQUEST_URI} [P,QSA,L]
應該轉換為
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;
}