1

I have a troble with NGINX that makes me insane. I have Ubuntu 12.04.3 LTS on VirtualBox. I installed NGINX-MYSQL-PHP following these instructions:

  1. install php5-fpm;
  2. set cgi.fix_pathinfo=0 in /etc/php5/fpm/php.ini;
  3. set listen = var/run/php5-fpm.sock in /etc/php5/fpm/pool.d/www.conf

Then I set in /etc/nginx/sites-available/default:

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;
}

Both localhost and info.php are executed properly. Now i want to set up a local website and try to make some redirect. So I created a new folder in /usr/share/nginx/www called mywebsite, put a index.php inside with a echo instruction, a php script i make redirect to, added a new config file in sites-available and a link to that in sites-enabled, and disable application/octet-stream in nginx.conf in order to avoid to downloading the script file. So i set my new config file as following:

server {
    listen   80;

    root /usr/share/nginx/www/mysite;
    index index.php index.html index.htm;

    server_name www.mysite.com;

    location / {
      try_files $uri $uri/ /index.html;
      if (!-e $request_filename){
        rewrite ^/location/([0-9]+)/device/([0-9]+)/senseddata/$ /senseddata.php break;
        rewrite ^/location/([0-9]+)/device/([0-9]+)/senseddata/lasthour/$ /senseddata.php break;
        rewrite ^/location/([0-9]+)/device/([0-9]+)/senseddata/daily/$ /senseddata.php break;
      }
    }

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;

    location = /50x.html {
       root /usr/share/nginx/www;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
       fastcgi_pass unix:/var/run/php5-fpm.sock;
       fastcgi_index index.php;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       include fastcgi_params;
       try_files $uri =404;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
    deny all;
   }
}

So what happened after all? Index.php is executed properly. Rewrite is done well but the script is not executed. I only see the entire code i wrote displayed in the browser. What's the problem? Thanks in advance.

4
  • Is your nginx works stand alone or reverse proxy with apache ? Looks like your nginx is not having a valid php handler Commented Jan 15, 2014 at 8:03
  • what do you get when you run sudo service php5-fpm status ? Commented Jan 16, 2014 at 12:37
  • Thanks for your answers. NGINX works alone and the php5-fpm status is running. By the way, I solved the problem writing LAST insted of BREAK in rewrite lines. I don't know the difference between the two words (maybe someone could explain) but it fix the problem. And it's good. Thank you very much. Commented Jan 17, 2014 at 10:45
  • I have same problem, and my config file same as @pasha-proton mentioned in his answer, still not working for me. Commented Oct 29, 2015 at 8:20

2 Answers 2

1

Also check the opening tags in your PHP-files. If you use <? instead of <?php and the short tag is not available, the PHP-code will not be parsed and you will see the source.

Sign up to request clarification or add additional context in comments.

Comments

0

Try add fastcgi_split_path_info ^(.+\.php)(/.+)$; to location ~ \.php$ and restart nginx.

Example from my config

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.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;
}

1 Comment

I have same problem, and my config file same as you mention, still not working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.