How can I do nginx rewrite?

I'm having difficulty configuring my nginx server to be a Python and PHP-based app server. Could you help me with my configuration nginx.conf file?

  • Here's how I'd like the server configured: I'd like my nginx server to determine the URL of the incoming request and redirect it to static content. If the request is for a PHP resource, I'd like the server to redirect to my PHP FastCGI process. If the URL isn't listed in the nginx.conf file (via the location blocks), I'd like the server to pass the request to my WSGI FastCGI process. Could you help me configure the below nginx.conf file to accomplish this? Currently, when I go to the testsite URL, I get an error message. It's as if my index statement isn't working. However, if I go to testsite URL/index.html, I get the file without any problem. Please help. Here is my nginx.conf file: user www-data; worker_processes 4; pid /run/nginx.pid; events {         worker_connections 768; } http {         sendfile on;         tcp_nopush on;         tcp_nodelay on;         keepalive_timeout 65;         types_hash_max_size 2048;         index index.php index.htm index.html;         server_tokens off;         include /etc/nginx/mime.types;         default_type application/octet-stream;         access_log /var/log/nginx/access.log;         error_log /var/log/nginx/error.log;         gzip on;         gzip_disable "msie6";     server {         listen 80;             server_name testsite(dot)com *.testsite(dot)com;             root /data/testsite(dot)com/html/;             access_log /var/log/nginx/testsite(dot)com.access.log;             error_log /var/log/nginx/http://testsite.com.error.log;             error_page 404 /error/404.html;             location @app {                     uwsgi_pass unix:///tmp/testsite(dot)com.sock;                     include uwsgi_params;             }             location / {                     try_files $uri @app =404;             }             # pass the PHP scripts to FastCGI server             location ~ \.php$ {                     try_files $uri =404;                     fastcgi_pass unix:/var/run/php5-fpm.sock;                     fastcgi_index index.php;                     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;                     include fastcgi_params;             }     } }

  • Answer:

    I'm not sure how the server even started with that. try_files $uri $uri/ @app; First it will try $uri as a file.  If that doesn't work, it will try $uri as a directory, checking for index.html, index.php, etc.  The last item in try_files is different from the others: it's like a rewrite.  If the previous files fail, try_files passes along processing to the last item.  In other words, it will look for a matching location block, whereas for the non-final items, it's confined to the current location block.  This means that @app has to be the last item.  =404 also has to be the last item, so you can't have both.  Since we're forwarding to @app--not checking if it exists--the 404 isn't necessary anyway. location ~ [^.]\.php(?:/|$) Don't treat a file named .php as a PHP file: it's just a hidden file named php.  Also, allow things like /index.php/banana.  You'll need to add fastcgi_splitpath: you shouldn't have a problem finding a good example for that. fastcgi_param needs to go after the include. You want your fastcgi_param to overwrite the one in the include. In the PHP location block, you probably want: try_files $uri @app; This way, if a PHP file doesn't exist, it will continue on to your Python application. If you don't want to rely on the Host header--in other words, you want this to be the default website for port 80--use: listen 80 default_server;

Paul Buonopane at Quora Visit the source

Was this solution helpful to you?

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.