Nginx mod_rewrite

Posted on Friday, February 14, 2014


I have a write up on using nginx and php at http://www.whiteboardcoder.com/2014/02/nginx-and-python.html  but at the time I did not have a need for what would be called a mod_rewrite in Apache.
I have a wordpress site I set up for my business and the url links for the pages are of the form.



/?page_id=14

I decided I wanted to change one of these to a more readable URL.    


To do this in Wordpress select Permalinks from the Settings tool.


Select Post name to have the URLS use the name of the post.



Click Save Changes.



This does update all my links on the Wordpress site to nice URLS I can read, but they do not work I get the 404 Not Found Error.

This is due to a bad nginx.conf file.  I forgot to add a rewrite  (equivalent to Apache mod_rewrite)

Edit the nginx.conf file


> sudo vi /etc/nginx/nginx.conf


Add the following to the server section


    #rwrite invalid request
    if (!-e $request_filename)
    {
        rewrite ^(.+)$ /index.php?q=$1 last;
    }


And restart nginx

> sudo /etc/init.d/nginx restart





Now the page comes up just fine J


For completeness here is my nginx.conf hope it's of some use to someonee.


user www-data;
worker_processes 4;
pid /var/run/nginx.pid;



events {
  worker_connections 1024;
  use epoll;
  multi_accept on;
}


http {

  include /etc/nginx/mime.types;
  index    index.html index.htm;

  default_type application/octet-stream;

  sendfile     on;
  tcp_nopush   on;
  tcp_nodelay on;
  server_names_hash_bucket_size 128;
  keepalive_timeout    70;
  types_hash_max_size 2048;


  gzip on;
  gzip_disable "msie6";

  log_format  main_fmt '$remote_addr - $remote_user [$time_local]  $status '
    '"$request" $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';


  server {
    listen       80;
    listen       8080;
    server_name *.10x13.com 10x13.com;
    access_log   /www/log/10x13/access.10x13.com.log  main_fmt;
    error_log    /www/log/10x13/error.10x13.com.log;
    client_max_body_size 25M;

    # doc root
    root /www/var/10x13/www;
    index index.html index.htm index.php;
    try_files $uri $uri/ /index.php;

    #rwrite invalid request
    if (!-e $request_filename)
    {
        rewrite ^(.+)$ /index.php?q=$1 last;
    }

    location / {
       autoindex                   on;
       autoindex_exact_size        off;
       autoindex_localtime         on;
    }

    location ~ \.php$ {
      fastcgi_pass    127.0.0.1:9000;
      fastcgi_index   index.php;
      fastcgi_param   SCRIPT_FILENAME /www/var/10x13/www$fastcgi_script_name;
      include         fastcgi_params;
    }
    location ~ \.py$ {
      include uwsgi_params;
      uwsgi_param UWSGI_FILE $request_filename;
      uwsgi_param UWSGI_TOUCH_RELOAD $request_filename;
      uwsgi_param SCRIPT_NAME $uri;
      if (-f $request_filename) {
          uwsgi_pass 127.0.0.1:9900;
      }
    }
  }

}





References


1 comment:

  1. This comment has been removed by the author.

    ReplyDelete