A redirect to a main site
to something like this:RewriteCond %{HTTP_HOST} nginx.org RewriteRule (.*) http://www.nginx.org$1
server { listen 80; server_name www.nginx.org nginx.org; if ($http_host = nginx.org) { rewrite (.*) http://www.nginx.org$1; } ... }
This is a wrong, cumbersome, and ineffective way. The right way is to define a separate server for nginx.org:
server { listen 80; server_name nginx.org; return 301 http://www.nginx.org$request_uri; } server { listen 80; server_name www.nginx.org; ... }
On versions prior to 0.9.1, redirects can be made with:rewrite ^ http://www.nginx.org$request_uri?;
Another example. Instead of the “upside-down” logic “all that is not nginx.com and is not www.nginx.com”:
one should simply define nginx.com, www.nginx.com, and “everything else”:RewriteCond %{HTTP_HOST} !nginx.com RewriteCond %{HTTP_HOST} !www.nginx.com RewriteRule (.*) http://www.nginx.com$1
server { listen 80; server_name nginx.com www.nginx.com; ... } server { listen 80 default_server; server_name _; return 301 http://nginx.com$request_uri; }
On versions prior to 0.9.1, redirects can be made with:rewrite ^ http://nginx.com$request_uri?;
Converting Mongrel rules
should be converted toDocumentRoot /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]
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; }