How to remove .html from URLs with nginx rewrites
This will rewrite http://xxx.com/porn to http://xxx.com/porn.html behind the scenes:
1 location / { 2 # break if URI has .html extension 3 if ($request_filename ~* ^.+.html$) { 4 break; 5 } 6 # add .html to URI and serve file, directory, or symlink if it exists 7 if (-e $request_filename.html) { 8 rewrite ^/(.*)$ /$1.html last; 9 break; 10 } 11 }
This URL will also work http://xxx.com/porn/
How to remove trailing slash with nginx rewrites
1 # Remove trailing slash by doing a 301 redirect 2 rewrite ^/(.*)/$ /$1 permanent;
How to install Nginx from source, Ruby Enterprise Edition, and Phusion Passenger
Ruby Enterprise Edition:
1 cd /usr/local/src 2 wget thttp://rubyforge.org/frs/download.php/66162/ruby-enterprise-1.8.7-2009.10.tar.gz 3 tar zxvf ruby-enterprise-1.8.7-2009.10.tar.gz 4 ./ruby-enterprise-1.8.7-2009.10/installer 5 6 ln -fs /opt/ruby-enterprise-1.8.7-2009.10 /opt/ruby-enterprise/ 7
Nginx:
1 wget -P http://sysoev.ru/nginx/nginx-0.7.63.tar.gz 2 tar -xzf nginx-0.7.63.tar.gz 3
Phusion Passenger:
1 gem install passenger 2 /opt/ruby-enterprise/bin/passenger-install-nginx-module --auto --nginx-source-dir=/tmp/nginx-0.7.63 --prefix=/opt/nginx --extra-configure-flags=--with-http_ssl_module 3
nginx, fastcgi and wordpress
Some pitfalls I ran into.
CSS is served up as text/html by fastcgi to nginx. Determine this by turning on “Net” in Firebug. Firefox in it’s standard compliance isn’t able to view the style sheet and therefore any images that may be defined in it. Solution; Serve up static stuff as static stuff through nginx:
1 server { 2 # ... abbreviated 3 location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mov)$ { 4 root /home/wordpress/wordpress; 5 } 6 # ... abbreviated 7 }
You receive the error “The plain HTTP request was sent to HTTPS port”. Solution: Turn on HTTPS in fastcgi by sending the follwing parameter to it in nginx.conf.
1 server { 2 # ... abbreviated 3 location / { 4 # ... abbreviated 5 fastcgi_param HTTPS on; 6 # ... abbreviated 7 } 8 # ... abbreviated 9 }
You get the FTP dialogue when trying to upload a new theme. Solution: Check that the process which runs fastcgi has write privileges into the wordpress folder.
Logging nginx to remote loghost with syslog-ng.
Nginx does not support syslog by default, so you have to patch it with a third party module. This snippet relies on that you have configured xstow. See this snippet for instructions on xstow configuration.
1 cd /tmp 2 wget http://sysoev.ru/nginx/nginx-0.6.32.tar.gz 3 tar zxvf nginx-0.6.32.tar.gz 4 mv nginx-0.6.32 src-0.6.32-orig 5 wget "http://wiki.codemongers.com/NginxModules?action=AttachFile&do=get&target=syslog.patch" -O syslog.patch 6 patch -p0 < syslog.patch 7 cd src-0.6.32-orig 8 ./configure --prefix=/usr/local/stow/nginx --with-syslog 9 make 10 sudo make install 11 cd /usr/local/stow 12 sudo xstow nginx
I-am-a-noob-at-syslog-disclaimer: This might be a totally wrong way to configure the server and client(s), so it is subject for refinement. In my experience it works though.
Configuring the client. Add the following lines to the end of /etc/syslog-ng/syslog-ng.conf and restart syslog-ng with /etc/init.d/syslog-ng restart. Nginx logs in facility local5 and the hostname of the loghost is “loghost”. You could just as well use the IP of the loghost.
1 filter f_local5 { facility(local5); }; 2 destination d_loghost {tcp("loghost" port(514));}; 3 log { source(s_all); filter(f_local5); destination(d_loghost); };
Configuring the server. Add the following lines to the end of /etc/syslog-ng/syslog-ng.conf and restart syslog-ng with /etc/init.d/syslog-ng restart. Also if you run a cluster of nginx servers it might be wise to put all the output in one file, instead of separate files per host.
1 source s_remote { tcp(); }; 2 destination d_clients { file("/var/log/HOSTS/nginx.$HOST"); }; 3 log { source(s_remote); destination(d_clients); };
Test the logging by running this from the client.
1 logger -p local5.info Hubbabubba