NGINX not executing php code in second site from a different folder

March 31, 2015 6.5k views
Nginx Linux Basics Server Optimization PHP
mzuker
By:
mzuker

Hello,

Our client have NGINX running on ubuntu, the main site is running wordpress perfectly in www.domain.com, but now we installed another PHP app in a folder /app,
so when visiting www.domain.com/app i can see all the content from the app, but cant execute PHP code, so inside the app all the code is in this format:

<? echo $semana_actual['total'] ?>


We are using this setup:



# You may add here your
# server {
#   ...
# }
# statements for each of your virtual hosts to this file

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /var/www/html;
    index index.html index.php index.htm;

    # Make site accessible from http://localhost/
    server_name cercis.cl;

    location / {
                # try_files $uri $uri/ =404;
                try_files $uri $uri/ /index.php?q=$uri&$args;
        }


    location /app {
                try_files $uri $uri/  /app/index.php?q=$uri&$args;
    }



    # Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
    #location /RequestDenied {
    #   proxy_pass http://127.0.0.1:8080;    
    #}

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

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_read_timeout 300;

    }

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


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#   listen 8000;
#   listen somename:8080;
#   server_name somename alias another.alias;
#   root html;
#   index index.html index.htm;
#
#   location / {
#       try_files $uri $uri/ =404;
#   }
#}


# HTTPS server
#
#server {
#   listen 443;
#   server_name localhost;
#
#   root html;
#   index index.html index.htm;
#
#   ssl on;
#   ssl_certificate cert.pem;
#   ssl_certificate_key cert.key;
#
#   ssl_session_timeout 5m;
#
#   ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
#   ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
#   ssl_prefer_server_ciphers on;
#
#   location / {
#       try_files $uri $uri/ =404;
#   }
#}

3 Answers

Everything looks ok, but this looks redundant, try removing it and reloading nginx:

    location /app {
                try_files $uri $uri/  /app/index.php?q=$uri&$args;
    }

root@nginx:~# ls -al /var/www/html/app
total 472
drwxr-xr-x 8 www-data www-data   4096 Mar 31 11:26 .
drwxr-xr-x 7 www-data www-data   4096 Mar 31 10:40 ..
-rw-r--r-- 1 www-data www-data  13629 Mar 31 10:41 agregarcampana.php
-rw-r--r-- 1 www-data www-data   4783 Mar 31 10:41 agregarcompanias.php
-rw-r--r-- 1 www-data www-data   8106 Mar 31 10:41 agregarcuentas.php
-rw-r--r-- 1 www-data www-data   4407 Mar 31 10:41 agregarpuntodeventa.php
-rw-r--r-- 1 www-data www-data   5201 Mar 31 10:41 agregarvehiculo.php
-rw-r--r-- 1 www-data www-data  10779 Mar 31 10:41 agregarvendedor.php
-rw-r--r-- 1 www-data www-data 101785 Mar 31 10:41 ajax.process.php
drwxr-xr-x 6 www-data www-data   4096 Mar 31 10:42 assets
drwxr-xr-x 7 www-data www-data   4096 Mar 31 10:45 backup_cercis
-rw-r--r-- 1 www-data www-data  10685 Mar 31 10:41 campanas.php
-rw-r--r-- 1 www-data www-data  11424 Mar 31 10:41 companias.php
-rw-r--r-- 1 www-data www-data  11159 Mar 31 10:41 cuentas.php
-rw-r--r-- 1 www-data www-data   8316 Mar 31 10:41 detalle.php
-rw-r--r-- 1 www-data www-data  14132 Mar 31 10:41 estado-rendiciones.php
-rw-r--r-- 1 www-data www-data   5549 Mar 31 10:41 footer.php
-rw-r--r-- 1 www-data www-data   5810 Mar 31 10:41 ganancia_cercis.php
-rw-r--r-- 1 www-data www-data  13355 Mar 31 10:41 header.php
-rw-r--r-- 1 www-data www-data   5841 Mar 31 10:41 historial_importaciones.php
-rw-r--r-- 1 www-data www-data   7630 Mar 31 10:41 historial_rendiciones.php
drwxr-xr-x 7 www-data www-data   4096 Mar 31 10:42 images
drwxr-xr-x 2 www-data www-data   4096 Mar 31 10:41 imgusuarios
-rw-r--r-- 1 www-data www-data   5988 Mar 31 10:41 importacion_datos.php
drwxr-xr-x 3 www-data www-data   4096 Mar 31 10:41 includes
-rw-r--r-- 1 www-data www-data  12277 Mar 31 10:41 index.php
-rw-r--r-- 1 www-data www-data     16 Mar 31 10:41 info.php
-rw-r--r-- 1 www-data www-data   7317 Mar 31 10:41 lockscreen.php
-rw-r--r-- 1 www-data www-data   7960 Mar 31 10:41 login.php
-rw-r--r-- 1 www-data www-data    130 Mar 31 10:41 logout.php
-rw-r--r-- 1 www-data www-data   2046 Mar 31 10:41 mantenimiento.php
-rw-r--r-- 1 www-data www-data     14 Mar 31 11:26 phpinfo.php
-rw-r--r-- 1 www-data www-data    625 Mar 31 10:41 Plantilla_contenido.php
-rw-r--r-- 1 www-data www-data   8754 Mar 31 10:41 polizas_importar_paso_02.php
-rw-r--r-- 1 www-data www-data  11631 Mar 31 10:41 precios_companias.php
-rw-r--r-- 1 www-data www-data  13512 Mar 31 10:41 precio_vendedores.php
-rw-r--r-- 1 www-data www-data  12786 Mar 31 10:41 puntosdeventa.php
-rw-r--r-- 1 www-data www-data  10154 Mar 31 10:41 rendiciones.php
-rw-r--r-- 1 www-data www-data   4326 Mar 31 10:41 respuesta_vehiculos.php
-rw-r--r-- 1 www-data www-data     59 Mar 31 10:41 robots.txt
drwxr-xr-x 4 www-data www-data   4096 Mar 31 10:41 uploads
-rw-r--r-- 1 www-data www-data   9655 Mar 31 10:41 vehiculos.php
-rw-r--r-- 1 www-data www-data  19683 Mar 31 10:41 vendedores.php

Everything seems OK.

You may check /etc/php5/fpm/php.ini's short_open_tag config value.
It should be On for php short tag to work.

If it is correct, you can try to override access_log inside fpm block.

location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_read_timeout 300;

        access_log /somewhere/writable/for/www-data/php-access.log;
}

After that access a php file and a non php under the app folder and also the root folder.
If there is no log entry in php-access.log, nginx is not redirecting php request to fpm.

Have another answer? Share your knowledge.