K
K
koltykov2014-07-27 09:26:26
PHP
koltykov, 2014-07-27 09:26:26

NGINX and PHP-FPM do not process include code (shows source in browser). Why?

I'm setting up a new server with nginx 1.6.0 and PHP 5.5.15 (PHP-FPM). I encountered such a problem that PHP files that are redirected to location in NGINX are not processed by PHP, and the source code is shown in the browser.
Domain config:

server {
    listen 80;
    server_name www.domain.com domain.com;

    root /var/www/domain.com/;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    location /cms {
  rewrite ^(.*)$ /index.php?q=$1 last;
    }
    location /tags/ {
  rewrite ^/(.+)$ /cms/index.php?q=$1 last;
    }

    include /etc/nginx/templates/default;
    include /etc/nginx/templates/php;
}

templates/php code:
# Передаём обработку PHP-скриптов PHP-FPM
location ~ \.php$ {
    try_files $uri =404; 
    #PHP-FPM слушает на Unix сокете
    fastcgi_pass   unix:/tmp/wwwpool.sock;

    #Использовать cache зона one
    fastcgi_cache one;

    #Помещать страницу в кеш, после 3-х использований. 
    fastcgi_cache_min_uses 3;

    #Кешировать перечисленные ответы
    fastcgi_cache_valid 200 301 302 304 5m;

    #Формат ключа кеша - по этому ключу nginx находит правильную страничку
    fastcgi_cache_key "$request_method|$host|$request_uri";

    fastcgi_index index.php;
    fastcgi_intercept_errors on; # только на период тестирования

    # Включаем параметры из /etc/nginx/fastcgi_param
    include fastcgi_params;

    # Путь к скрипту, который будет передан в php-fpm
    fastcgi_param       SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_ignore_client_abort     off;
}

nginx.conf config:
user  www-data;
worker_processes auto;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
    use epoll;
}

http {
    fastcgi_cache_path /tmp/fcgi-cache/ levels=1:2   keys_zone=one:10m;
    server_tokens off;

    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    #default_type text/html;

    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;
    error_log /var/log/nginx/error.log crit;

    sendfile    on;
    tcp_nopush  on;
    tcp_nodelay on; 

    keepalive_timeout 20;
    client_body_timeout 30;
    send_timeout 20;
    client_max_body_size 10m;

    reset_timedout_connection on; 

    ##
    # Gzip Settings
    ##
    gzip on;
    gzip_min_length	2100;
    gzip_disable	"msie6";
    gzip_proxied	any;
    gzip_comp_level	4;
    gzip_types 		text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    gzip_vary		on; 

    include /etc/nginx/sites-enabled/*;
}

In particular, when you call any page domain.com/tags/exampl1 in the browser, the source code of the page /cms/index.php is shown. Those. <?PHP etc.
In the PHP.ini settings, the directive short_open_tag = On

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor, 2014-07-27
@koltykov

Try removing the try_files line from this construct:

location ~ \.php$ {
    try_files $uri =404;

Is short_open_tag enabled in php.ini ? What happens if you create a file with the content: and try to open it through a browser?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question