Nginx serves php files as plain text (Mod_Rewrite)

Jake asked:

Grr..

Okay, I have this problem…

So there is no .htaccess (Mod_Rewrite) in Nginx.. So I’ve converted my .htaccess to a format which can be read and placed in the default.conf.

Original htaccess:

Options -Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?/$0 [PT,L]

Converted htaccess to Nginx format:

# nginx configuration
autoindex off;
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?/$0 break;
}
}

Right… Instant problem there.. Nginx doesn’t support the arguments $0 so I’ve changed this to $uri

Problem I have now.. When I visit my website http://vibeshed.com/admin I get the below result:
(Before btw I was just downloading the file but I removed the default mime type).

See Image (Because I don’t have rep points to embed http://i.stack.imgur.com/sg4h2.png

Lil help guys?

My sites default.conf is:

#
# The default server
#
server {
    listen       80;
    server_name vibeshed.com;


    location / {
        root   /usr/share/nginx/html;
        index index.php  index.html index.htm;
autoindex on;
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?/$uri break;
}
    }

    error_page  404              /404.html;
    location = /404.html {
        root   /usr/share/nginx/html;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ .php$ {
        root           /usr/share/nginx/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

My Nginx Config is:

#######################################################################
#
# This is the main Nginx configuration file.  
#
# More information about the configuration options is available on 
#   * the English wiki - http://wiki.nginx.org/Main
#   * the Russian documentation - http://sysoev.ru/nginx/
#
#######################################################################

#----------------------------------------------------------------------
# Main Module - directives that cover basic functionality
#
#   http://wiki.nginx.org/NginxHttpMainModule
#
#----------------------------------------------------------------------

user              nginx;
worker_processes  4;

error_log  /var/log/nginx/error.log;
#error_log  /var/log/nginx/error.log  notice;
#error_log  /var/log/nginx/error.log  info;

pid        /var/run/nginx.pid;


#----------------------------------------------------------------------
# Events Module 
#
#   http://wiki.nginx.org/NginxHttpEventsModule
#
#----------------------------------------------------------------------

events {
    worker_connections  1024;
}


#----------------------------------------------------------------------
# HTTP Core Module
#
#   http://wiki.nginx.org/NginxHttpCoreModule 
#
#----------------------------------------------------------------------

http {
    include       /etc/nginx/mime.types;

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

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    #
    # The default server
    #
    server {
        listen       80;
        server_name  _;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /usr/share/nginx/html;
            index  index.php index.html index.htm;
        }

        error_page  404              /404.html;
        location = /404.html {
            root   /usr/share/nginx/html;
        }

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ .php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ .php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

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

    # Load config files from the /etc/nginx/conf.d directory
    include /etc/nginx/conf.d/*.conf;

}

The ultimate question is how to I get /admin (Driven by the mod_rewite / Or NGinx Alternative) to display as a PHP File and not some text or an application/octet-stream.

BTW I’ve installed the php-fpm module so php files do render fine.. It’s just they aren’t rendering when driven by the rewrite in place.

Thanks very much!


Here is also the entirety of my Nginx Error Log.

2014/12/09 04:05:35 [error] 23378#0: *6796 open() "/usr/share/nginx/html/cgi-bin/authLogin.cgi" failed (2: No such file or directory), client: 210.3.228.123, server: vibeshed.com, request: "GET /cgi-bin/authLogin.cgi HTTP/1.1", host: "127.0.0.1"
2014/12/09 05:09:21 [error] 23378#0: *6810 open() "/usr/share/nginx/html/cgi-bin/authLogin.cgi" failed (2: No such file or directory), client: 84.61.245.6, server: vibeshed.com, request: "GET /cgi-bin/authLogin.cgi HTTP/1.1", host: "127.0.0.1"
2014/12/09 05:36:06 [error] 23378#0: *6813 open() "/usr/share/nginx/html/cgi-bin/authLogin.cgi" failed (2: No such file or directory), client: 178.193.100.96, server: vibeshed.com, request: "GET /cgi-bin/authLogin.cgi HTTP/1.1", host: "127.0.0.1"
2014/12/09 06:10:29 [error] 23378#0: *6815 open() "/usr/share/nginx/html/cgi-bin/authLogin.cgi" failed (2: No such file or directory), client: 88.175.95.10, server: vibeshed.com, request: "GET /cgi-bin/authLogin.cgi HTTP/1.1", host: "127.0.0.1"
2014/12/09 08:40:41 [error] 23378#0: *7270 open() "/usr/share/nginx/html/cgi-bin/authLogin.cgi" failed (2: No such file or directory), client: 14.136.83.67, server: vibeshed.com, request: "GET /cgi-bin/authLogin.cgi HTTP/1.1", host: "127.0.0.1"
2014/12/09 09:27:54 [error] 23378#0: *7284 open() "/usr/share/nginx/html/cgi-bin/authLogin.cgi" failed (2: No such file or directory), client: 118.163.106.140, server: vibeshed.com, request: "GET /cgi-bin/authLogin.cgi HTTP/1.1", host: "127.0.0.1"
2014/12/09 09:51:56 [error] 23379#0: *7454 open() "/usr/share/nginx/html/cgi-bin/authLogin.cgi" failed (2: No such file or directory), client: 59.127.84.8, server: vibeshed.com, request: "GET /cgi-bin/authLogin.cgi HTTP/1.1", host: "127.0.0.1"
2014/12/09 10:48:55 [error] 23379#0: *8009 open() "/usr/share/nginx/html/cgi-bin/authLogin.cgi" failed (2: No such file or directory), client: 88.159.106.164, server: vibeshed.com, request: "GET /cgi-bin/authLogin.cgi HTTP/1.1", host: "127.0.0.1"
2014/12/09 11:04:23 [error] 23379#0: *8102 open() "/usr/share/nginx/html/cgi-bin/authLogin.cgi" failed (2: No such file or directory), client: 95.89.236.195, server: vibeshed.com, request: "GET /cgi-bin/authLogin.cgi HTTP/1.1", host: "127.0.0.1"
2014/12/09 11:17:24 [error] 23379#0: *8103 open() "/usr/share/nginx/html/cgi-bin/authLogin.cgi" failed (2: No such file or directory), client: 80.219.188.234, server: vibeshed.com, request: "GET /cgi-bin/authLogin.cgi HTTP/1.1", host: "127.0.0.1"
2014/12/09 11:22:53 [error] 23379#0: *8104 open() "/usr/share/nginx/html/cgi-bin/authLogin.cgi" failed (2: No such file or directory), client: 203.186.126.186, server: vibeshed.com, request: "GET /cgi-bin/authLogin.cgi HTTP/1.1", host: "127.0.0.1"
2014/12/09 11:28:19 [error] 23379#0: *8105 open() "/usr/share/nginx/html/cgi-bin/authLogin.cgi" failed (2: No such file or directory), client: 212.41.105.10, server: vibeshed.com, request: "GET /cgi-bin/authLogin.cgi HTTP/1.1", host: "127.0.0.1"
2014/12/09 11:47:21 [error] 23379#0: *8108 open() "/usr/share/nginx/html/cgi-bin/authLogin.cgi" failed (2: No such file or directory), client: 60.249.117.137, server: vibeshed.com, request: "GET /cgi-bin/authLogin.cgi HTTP/1.1", host: "127.0.0.1"
2014/12/09 11:53:39 [error] 23379#0: *8109 open() "/usr/share/nginx/html/cgi-bin/authLogin.cgi" failed (2: No such file or directory), client: 77.41.149.118, server: vibeshed.com, request: "GET /cgi-bin/authLogin.cgi HTTP/1.1", host: "127.0.0.1"
2014/12/09 12:21:39 [error] 23379#0: *8110 open() "/usr/share/nginx/html/cgi-bin/authLogin.cgi" failed (2: No such file or directory), client: 24.226.239.76, server: vibeshed.com, request: "GET /cgi-bin/authLogin.cgi HTTP/1.1", host: "127.0.0.1"
2014/12/09 14:18:33 [error] 23382#0: *8162 open() "/usr/share/nginx/html/cgi-bin/authLogin.cgi" failed (2: No such file or directory), client: 61.197.228.249, server: vibeshed.com, request: "GET /cgi-bin/authLogin.cgi HTTP/1.1", host: "127.0.0.1"
2014/12/09 15:15:43 [error] 23379#0: *8708 open() "/usr/share/nginx/html/cgi-bin/authLogin.cgi" failed (2: No such file or directory), client: 61.224.19.150, server: vibeshed.com, request: "GET /cgi-bin/authLogin.cgi HTTP/1.1", host: "127.0.0.1"
2014/12/09 15:33:48 [error] 23379#0: *8887 open() "/usr/share/nginx/html/8080" failed (2: No such file or directory), client: 111.123.180.44, server: vibeshed.com, request: "GET /8080 HTTP/1.1", host: "115.230.125.165:61254"
2014/12/09 15:33:48 [error] 23379#0: *8888 open() "/usr/share/nginx/html/8080" failed (2: No such file or directory), client: 111.123.180.44, server: vibeshed.com, request: "GET /8080 HTTP/1.1", host: "115.230.125.165:61254"
2014/12/09 16:11:27 [error] 23381#0: *9215 open() "/usr/share/nginx/html/r/www/cache/static/home/img/logos/nuomi_ade5465d.png" failed (2: No such file or directory), client: 125.64.35.67, server: _, request: "GET http://s1.bdstatic.com/r/www/cache/static/home/img/logos/nuomi_ade5465d.png HTTP/1.1", host: "s1.bdstatic.com"
2014/12/09 16:22:33 [error] 23381#0: *9216 open() "/usr/share/nginx/html/cgi-bin/authLogin.cgi" failed (2: No such file or directory), client: 80.245.236.31, server: vibeshed.com, request: "GET /cgi-bin/authLogin.cgi HTTP/1.1", host: "127.0.0.1"
2014/12/09 16:57:52 [error] 23381#0: *9219 open() "/usr/share/nginx/html/cgi-bin/authLogin.cgi" failed (2: No such file or directory), client: 88.159.101.77, server: vibeshed.com, request: "GET /cgi-bin/authLogin.cgi HTTP/1.1", host: "127.0.0.1"
2014/12/09 17:12:39 [error] 23381#0: *9221 open() "/usr/share/nginx/html/r/www/cache/static/home/img/logos/nuomi_ade5465d.png" failed (2: No such file or directory), client: 125.64.35.67, server: vibeshed.com, request: "GET http://s1.bdstatic.com/r/www/cache/static/home/img/logos/nuomi_ade5465d.png HTTP/1.1", host: "s1.bdstatic.com"
2014/12/09 19:01:42 [error] 23379#0: *9235 open() "/usr/share/nginx/html/user/soapCaller.bs" failed (2: No such file or directory), client: 216.20.131.111, server: _, request: "GET /user/soapCaller.bs HTTP/1.1", host: "146.185.171.71"
2014/12/09 19:05:09 [error] 23379#0: *9236 open() "/usr/share/nginx/html/cgi-bin/authLogin.cgi" failed (2: No such file or directory), client: 46.107.209.83, server: vibeshed.com, request: "GET /cgi-bin/authLogin.cgi HTTP/1.1", host: "127.0.0.1"
2014/12/09 19:57:38 [error] 18171#0: *9 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 82.15.196.119, server: _, request: "GET /favicon.ico HTTP/1.1", host: "www.vibeshed.com"
2014/12/09 20:01:09 [error] 1445#0: *30 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 82.15.196.119, server: _, request: "GET /favicon.ico HTTP/1.1", host: "www.vibeshed.com"
2014/12/09 20:02:00 [error] 1445#0: *1 FastCGI sent in stderr: "PHP message: PHP Warning:  Unknown: failed to open stream: Permission denied in Unknown on line 0
Unable to open primary script: /usr/share/nginx/html/phpinfo.php (Permission denied)" while reading response header from upstream, client: 82.15.196.119, server: vibeshed.com, request: "GET /phpinfo.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "vibeshed.com"
2014/12/09 20:02:02 [error] 1445#0: *1 FastCGI sent in stderr: "PHP message: PHP Warning:  Unknown: failed to open stream: Permission denied in Unknown on line 0
Unable to open primary script: /usr/share/nginx/html/phpinfo.php (Permission denied)" while reading response header from upstream, client: 82.15.196.119, server: vibeshed.com, request: "GET /phpinfo.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "vibeshed.com"
2014/12/09 20:02:02 [error] 1445#0: *1 FastCGI sent in stderr: "PHP message: PHP Warning:  Unknown: failed to open stream: Permission denied in Unknown on line 0
Unable to open primary script: /usr/share/nginx/html/phpinfo.php (Permission denied)" while reading response header from upstream, client: 82.15.196.119, server: vibeshed.com, request: "GET /phpinfo.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "vibeshed.com"
2014/12/09 20:02:03 [error] 1445#0: *1 FastCGI sent in stderr: "PHP message: PHP Warning:  Unknown: failed to open stream: Permission denied in Unknown on line 0
Unable to open primary script: /usr/share/nginx/html/phpinfo.php (Permission denied)" while reading response header from upstream, client: 82.15.196.119, server: vibeshed.com, request: "GET /phpinfo.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "vibeshed.com"
2014/12/09 20:02:03 [error] 1445#0: *1 FastCGI sent in stderr: "PHP message: PHP Warning:  Unknown: failed to open stream: Permission denied in Unknown on line 0
Unable to open primary script: /usr/share/nginx/html/phpinfo.php (Permission denied)" while reading response header from upstream, client: 82.15.196.119, server: vibeshed.com, request: "GET /phpinfo.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "vibeshed.com"
2014/12/09 20:02:04 [error] 1445#0: *1 FastCGI sent in stderr: "PHP message: PHP Warning:  Unknown: failed to open stream: Permission denied in Unknown on line 0
Unable to open primary script: /usr/share/nginx/html/phpinfo.php (Permission denied)" while reading response header from upstream, client: 82.15.196.119, server: vibeshed.com, request: "GET /phpinfo.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "vibeshed.com"
2014/12/09 20:02:04 [error] 1445#0: *1 FastCGI sent in stderr: "PHP message: PHP Warning:  Unknown: failed to open stream: Permission denied in Unknown on line 0
Unable to open primary script: /usr/share/nginx/html/phpinfo.php (Permission denied)" while reading response header from upstream, client: 82.15.196.119, server: vibeshed.com, request: "GET /phpinfo.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "vibeshed.com"
2014/12/09 20:02:05 [error] 1445#0: *1 FastCGI sent in stderr: "PHP message: PHP Warning:  Unknown: failed to open stream: Permission denied in Unknown on line 0
Unable to open primary script: /usr/share/nginx/html/phpinfo.php (Permission denied)" while reading response header from upstream, client: 82.15.196.119, server: vibeshed.com, request: "GET /phpinfo.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "vibeshed.com"
2014/12/09 20:02:05 [error] 1445#0: *1 FastCGI sent in stderr: "PHP message: PHP Warning:  Unknown: failed to open stream: Permission denied in Unknown on line 0
Unable to open primary script: /usr/share/nginx/html/phpinfo.php (Permission denied)" while reading response header from upstream, client: 82.15.196.119, server: vibeshed.com, request: "GET /phpinfo.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "vibeshed.com"
2014/12/09 20:02:45 [error] 1445#0: *1 FastCGI sent in stderr: "PHP message: PHP Warning:  Unknown: failed to open stream: Permission denied in Unknown on line 0
Unable to open primary script: /usr/share/nginx/html/phpinfo.php (Permission denied)" while reading response header from upstream, client: 82.15.196.119, server: vibeshed.com, request: "GET /phpinfo.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "vibeshed.com"
2014/12/09 20:02:56 [error] 1445#0: *1 FastCGI sent in stderr: "PHP message: PHP Warning:  Unknown: failed to open stream: Permission denied in Unknown on line 0
Unable to open primary script: /usr/share/nginx/html/phpinfo.php (Permission denied)" while reading response header from upstream, client: 82.15.196.119, server: vibeshed.com, request: "GET /phpinfo.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "vibeshed.com"
2014/12/09 20:02:57 [error] 1445#0: *1 FastCGI sent in stderr: "PHP message: PHP Warning:  Unknown: failed to open stream: Permission denied in Unknown on line 0
Unable to open primary script: /usr/share/nginx/html/phpinfo.php (Permission denied)" while reading response header from upstream, client: 82.15.196.119, server: vibeshed.com, request: "GET /phpinfo.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "vibeshed.com"
2014/12/09 20:02:57 [error] 1445#0: *1 FastCGI sent in stderr: "PHP message: PHP Warning:  Unknown: failed to open stream: Permission denied in Unknown on line 0
Unable to open primary script: /usr/share/nginx/html/phpinfo.php (Permission denied)" while reading response header from upstream, client: 82.15.196.119, server: vibeshed.com, request: "GET /phpinfo.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "vibeshed.com"
2014/12/09 20:02:57 [error] 1445#0: *1 FastCGI sent in stderr: "PHP message: PHP Warning:  Unknown: failed to open stream: Permission denied in Unknown on line 0
Unable to open primary script: /usr/share/nginx/html/phpinfo.php (Permission denied)" while reading response header from upstream, client: 82.15.196.119, server: vibeshed.com, request: "GET /phpinfo.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "vibeshed.com"
2014/12/09 20:02:57 [error] 1445#0: *1 FastCGI sent in stderr: "PHP message: PHP Warning:  Unknown: failed to open stream: Permission denied in Unknown on line 0
Unable to open primary script: /usr/share/nginx/html/phpinfo.php (Permission denied)" while reading response header from upstream, client: 82.15.196.119, server: vibeshed.com, request: "GET /phpinfo.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "vibeshed.com"

My answer:


The problem here is that you used an online utility to “convert” from Apache to nginx, and none of these do very well.

if (!-e $request_filename){
rewrite ^(.*)$ /index.php?/$0 break;
}

This needs to be cut entirely, and replaced with try_files.

try_files $uri $uri/ /index.php?$uri;

You have also misplaced your root directive. It should be under server, not under location.


View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.