Nginx 以纯文本形式提供 php 文件(Mod_Rewrite)

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"

答案1

这里的问题是您使用在线实用程序将 Apache “转换”到 nginx,但效果都不太好。

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

需要将其全部删掉,并替换为try_files

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

您还放错了root指令。它应该位于 下server,而不是 下location

相关内容