3.3.6 Pretty URLs on nginx

nginx is a popular server that, like Lighttpd, uses less system resources. It's drawback is that it does not make use of .htaccess files like Apache and Lighttpd, so it is necessary to create those rewritten URLs in the site-available configuration. Depending upon your setup, you will have to modify this, but at the very least, you will need PHP running as a FastCGI instance.

server {
	listen   80;
	server_name www.example.com;
	rewrite ^(.*) http://example.com$1 permanent;
}

server {
	listen   80;
	server_name example.com;

	access_log /var/www/example.com/log/access.log;
	error_log /var/www/example.com/log/error.log;

	location / {
		root   /var/www/example.com/public/app/webroot/;
		index  index.php index.html index.htm;
		if (-f $request_filename) {
			break;
		}
		if (-d $request_filename) {
			break;
		}
		rewrite ^(.+)$ /index.php?q=$1 last;
	}

	location ~ .*\.php[345]?$ {
		include /etc/nginx/fcgi.conf;
		fastcgi_pass    127.0.0.1:10005;
		fastcgi_index   index.php;
		fastcgi_param SCRIPT_FILENAME /var/www/example.com/public/app/webroot$fastcgi_script_name;
	}
}
  1. server {
  2. listen 80;
  3. server_name www.example.com;
  4. rewrite ^(.*) http://example.com$1 permanent;
  5. }
  6. server {
  7. listen 80;
  8. server_name example.com;
  9. access_log /var/www/example.com/log/access.log;
  10. error_log /var/www/example.com/log/error.log;
  11. location / {
  12. root /var/www/example.com/public/app/webroot/;
  13. index index.php index.html index.htm;
  14. if (-f $request_filename) {
  15. break;
  16. }
  17. if (-d $request_filename) {
  18. break;
  19. }
  20. rewrite ^(.+)$ /index.php?q=$1 last;
  21. }
  22. location ~ .*.php[345]?$ {
  23. include /etc/nginx/fcgi.conf;
  24. fastcgi_pass 127.0.0.1:10005;
  25. fastcgi_index index.php;
  26. fastcgi_param SCRIPT_FILENAME /var/www/example.com/public/app/webroot$fastcgi_script_name;
  27. }
  28. }