Назад | Перейти на главную страницу

Nginx обслуживает файлы php как обычный текст (Mod_Rewrite)

В Nginx нет .htaccess (Mod_Rewrite). Итак, я преобразовал свой .htaccess в формат, который можно читать и поместить в default.conf.

Оригинальный htaccess:

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

Конвертировал htaccess в формат Nginx:

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

Верно ... Мгновенная проблема .. Nginx не поддерживает аргументы $ 0 поэтому я изменил это на $ uri

Проблема у меня сейчас ... Когда я захожу на свой сайт http://vibeshed.com/admin Я получаю следующий результат: (Раньше, кстати, я просто загружал файл, но я удалил тип mime по умолчанию).

См. Изображение:

Любая помощь?

Мои сайты default.conf:

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

Моя конфигурация Nginx:

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

}

Главный вопрос заключается в том, как получить / admin (управляемый альтернативой mod_rewite / или NGinx) для отображения в виде файла PHP, а не текста или приложения / потока октетов.

Кстати Я установил модуль php-fpm, поэтому файлы php обрабатываются нормально ... Просто они не отображаются при перезаписи на месте.

Огромное спасибо!


Вот и весь мой журнал ошибок Nginx.

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"

Проблема здесь в том, что вы использовали онлайн-утилиту для «конвертирования» из Apache в nginx, и ни одна из них не работает хорошо.

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

Это нужно полностью вырезать и заменить на try_files.

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

Вы также потеряли свой root директива. Это должно быть под server, не под location.