3

I've got a Nginx Web Server Setup with PHP and I want to move on Nginx and I want to convert these .htaccess rules to nginx.conf file:

RewriteRule ^blog(|/)$ /data/core/site/blog.php
RewriteRule ^blog/post(|/)$ /data/core/site/blogpost.php

So far this is what I have:

location /blog {
        rewrite ^(.*)$ /data/core/blog.php last;
    }

However if I visit the page (http://example.com/blog), It gives me the file to download, I want it to server the PHP and display content, how would I fix this?

Full Nginx configuration: (Using Winginx Package on Windows):

server {
    listen 127.0.0.1:80;
    server_name localhost;

    root home/localhost/public_html;

    index index.php;

    log_not_found off;
    #access_log logs/localhost-access.log;

    charset utf-8;

    location ~ /\. { deny all; }
    location = /favicon.ico { }
    location = /robots.txt { }

    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9054;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
        include fastcgi_params;
    }

    location /blog {
        rewrite ^(.*)$ /data/core/blog.php last;
    }
}
1
  • Okay, I've updated the question with the full Nginx Configuration Dec 13, 2014 at 13:17

2 Answers 2

2

Requests processing

The base rule to remember is: nginx serves a request with one location (you could emphase even more: and one location only).

Read: http://nginx.org/en/docs/http/request_processing.html

location matching

Read: location documentation

Based on your configuration, nginx will first match on the /blog prefix location, then on the \.php$ regex location and will eventually serves the request with the latter. With the configuration you provided, the script should not be downloaded as a raw file any more but should rather by sent over to PHP.

However, that does not mean your configuration is right: the request is not served by your /blog location, which is useless at the moment.

  1. It is a general good practice to avoid filtering requests with regex locations, which are order-based, which is bad (remember Apache directives order sensitivity nightmare?). To filter, use prefix locations instead which are longest-match-based. You can embed locations withing each other if you ultimately need a regex one.
  2. Why not directly putting your fastcgi* directives into the /blog location? Then, instead of using the $fastcgi_script_name variable (guessed from the location match that would be variants of /blog here), you could use fastcgi_param SCRIPT_FILENAME $document_root/data/core/blog.php. By the way, $fastcgi_script_filename already contained the starting /, no need to add one between the variables
  3. Avoid using redirections if you can avoid them. Avoid especially rewrite even more. Simple user redirections (URL rewriting done through redirection notifications sent to the client with HTTP status codes) can be done with return. What you did here was an internal redirection (done locally on the server): its only use was to change the URI then used for the SCRIPT_FILENAME parameter.

You could use for starters:

location /blog {
    fastcgi_pass 127.0.0.1:9054;
    fastcgi_param SCRIPT_FILENAME $document_root/data/core/blog.php;
    include fastcgi_params;

    # Useless here since SCRIPT_FILENAME will never be a directory indeed
    fastcgi_index index.php;

    location /blog/post {
        fastcgi_pass 127.0.0.1:9054;
        fastcgi_param SCRIPT_FILENAME $document_root/data/core/blogpost.php;
        include fastcgi_params;
    }
}
4
  • After trying this out it seems to have the same issue, it still gives me a file to download. Dec 13, 2014 at 16:26
  • Ensure your configuration file is OK with nginx -t and check that the reload went OK by monitoring the error log. If it is still not working, please provide information on your configuration and the requests you try Dec 13, 2014 at 20:34
  • Hello, I've managed to fix the issue, thanks for your help tho! :) I will post the answer shortly. Dec 14, 2014 at 9:23
  • 1
    Glad it worked. A little bit disappointed you did not follow any piece of advice I took quite some time to explain + added something also not recommended to make the whole even less ideal. Dec 15, 2014 at 17:59
1

This is the fix to my question, after doing alot of research on issues, I've found out that it was pretty straight forward:

server {
listen 127.0.0.1:80;
server_name virjox www.virjox;

root home/virjox/public_html;

index index.php;

log_not_found off;
#access_log logs/virjox-access.log;

charset utf-8;

sendfile on;

location ~ /\. { deny all; }
location = /favicon.ico { }
location = /robots.txt { }  
location ~ \.php$ {
    fastcgi_pass 127.0.0.1:9054;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
    include fastcgi_params;
    rewrite ^/(.*)\.html /$1\.php;
}
   location /blog {
     rewrite ^/blog(|/)$ /data/core/blog.php;
   }
}

and this works perfectly.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .