Nginx 和 PHP-FPM - 权限被拒绝 - Windows

Posted

技术标签:

【中文标题】Nginx 和 PHP-FPM - 权限被拒绝 - Windows【英文标题】:Nginx and PHP-FPM - Permission Denied - Windows 【发布时间】:2016-03-26 19:36:07 【问题描述】:

我正在尝试在 Windows 10 中使用 nginx 配置 php 服务器,但我遇到了一些问题。

我想做什么?

如果我访问本地主机,我会看到 phpinfo 页面(确定) 如果我访问 localhost/phpmyadmin 我看到访问被拒绝。 (失败) 如果我访问 localhost/laravel/public 我会看到 laravel 框架的欢迎页面。

目录结构

D:/Server/apps/phpmyadmin(phpmyadmin 应用程序) D:/Server/bin/nginx D:/服务器/bin/php D:/Server/conf/nginx/nginx.conf D:/Server/conf/php/php.ini D:/服务器/日志/nginx D:/服务器/日志/php D:/Server/www/index.php(phpinfo页面) D:/Server/www/laravel(laravel项目)

我尝试了什么? 我添加 phpmyadmin.app do windows hosts 文件并将另一个服务器块添加到 nginx.conf 并且可以工作,但我不想这样做。

错误日志

2015/12/20 14:14:14 [error] 6932#10736: *1 FastCGI sent in stderr: "PHP Warning:  Unknown: failed to open stream: Permission denied in Unknown on line 0" while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /phpmyadmin/ HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "localhost"

host.access.log

127.0.0.1 - - [20/Dec/2015:14:35:19 -0200] "GET /phpmyadmin/ HTTP/1.1" 403 46 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/47.0.2526.106 Safari/537.36" "-"
127.0.0.1 - - [20/Dec/2015:15:59:43 -0200] "GET /phpmyadmin/index.php HTTP/1.1" 404 564 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36" "-"
127.0.0.1 - - [20/Dec/2015:16:01:37 -0200] "GET / HTTP/1.1" 200 24385 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36" "-"
127.0.0.1 - - [20/Dec/2015:16:01:39 -0200] "GET /laravel/public/ HTTP/1.1" 200 402 "http://localhost/laravel/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36" "-"

nginx-start.bat

@echo off
echo Starting Nginx...
D:\Server\bin\hidec D:\Server\bin\nginx\nginx -c D:\Server\conf\nginx\nginx.conf -p D:\Server\bin\nginx
echo Starting PHP FastCGI...
D:\Server\bin\hidec D:\Server\bin\php\php-cgi -b 127.0.0.1:9000 -c D:\Server\conf\php\php.ini

nginx.conf

worker_processes  auto;

error_log  D:/Server/logs/nginx/error.log;

events 
    worker_connections  1024;
    multi_accept        off;


http 
    include       D:/Server/bin/nginx/conf/mime.types;
    default_type  application/octet-stream;

    fastcgi_temp_path       D:/Server/tmp/nginx/fastcgi;
    uwsgi_temp_path         D:/Server/tmp/nginx/uwsgi;
    scgi_temp_path          D:/Server/tmp/nginx/scgi;
    client_body_temp_path   D:/Server/tmp/nginx/client-body 1 2;
    proxy_temp_path         D:/Server/tmp/nginx/proxy;

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

    server_name_in_redirect off;
    server_tokens           off;

    server_names_hash_bucket_size 64;
    server_names_hash_max_size    512;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    types_hash_max_size 2048;

    client_body_buffer_size     64k;
    client_header_buffer_size   4k;
    client_max_body_size        8M;
    large_client_header_buffers 4 64k;

    client_body_timeout     10;
    client_header_timeout   10;
    keepalive_timeout       30;
    send_timeout            10;
    keepalive_requests      10;

    fastcgi_connect_timeout      60;
    fastcgi_send_timeout         120;
    fastcgi_read_timeout         300;
    fastcgi_buffer_size          64k;
    fastcgi_buffers              4 64k;
    fastcgi_busy_buffers_size    128k;
    fastcgi_temp_file_write_size 128k;

    gzip                on;
    gzip_buffers        16 8k;
    gzip_comp_level     5;
    gzip_http_version   1.0;
    gzip_min_length     1000;
    gzip_proxied        any;
    gzip_types

    text/css text/javascript text/xml text/plain text/x-component
    application/x-javascript application/javascript application/json application/xml application/rss+xml
    font/truetype font/opentype application/vnd.ms-fontobject
    image/svg+xml;
    gzip_vary           on;

    autoindex on;

    upstream php 
        server 127.0.0.1:9000;
    

    upstream php_pool 
        ip_hash;
        server 127.0.0.1:9000 weight=1 max_fails=3 fail_timeout=10s;
        server 127.0.0.1:9001 weight=1 max_fails=3 fail_timeout=10s;
        server 127.0.0.1:9002 weight=1 max_fails=3 fail_timeout=10s;
        server 127.0.0.1:9003 weight=1 max_fails=3 fail_timeout=10s;
    

    server 
        listen       127.0.0.1:80;
        server_name  localhost;
        root         D:/Server/www;

        log_not_found off;
        charset utf-8;

        access_log  D:/Server/logs/nginx/host.access.log  main;

        location / 
            index index.php index.html;
        

        location /phpmyadmin/ 
            alias          D:/Server/apps/phpmyadmin;
            fastcgi_pass   php;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $request_filename;
            fastcgi_param  REMOTE_ADDR $http_x_real_ip;
            include        D:/Server/bin/nginx/conf/fastcgi_params;
        

        error_page   500 502 503 504  /50x.html;
        location = /50x.html 
            root   html;
        

        location ~ \.php$ 
            try_files      $uri =404;
            fastcgi_pass   php;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  REMOTE_ADDR $http_x_real_ip;
            include        D:/Server/bin/nginx/conf/fastcgi_params;
        

        location ~* ^.+.(gif|ico|jpg|jpeg|png|flv|swf|pdf|mp3|mp4|xml|txt|js|css)$ 
            expires 30d;
            add_header Vary Accept-Encoding;
        

        if ($request_method !~ ^(GET|HEAD|POST)$ )  return 405; 

        location ~ /(\.ht|\.git|\.svn) 
            access_log off;
            log_not_found off;
            deny  all;
        
    

来自 Nginx HTTP Server 书:

别名上下文:位置。变量被接受。别名是您仅放置在位置块中的指令。它为 Nginx 分配不同的路径来检索特定请求的文档。例如,考虑以下配置: http server server_name localhost;根 /var/www/website.com/html;位置 /admin/ 别名 /var/www/locked/; 当收到http://localhost/ 的请求时,文件将从 /var/www/website.com/html/ 文件夹中提供。但是,如果 Nginx 收到http://localhost/admin/ 的请求,则用于检索文件的路径是 var/www/locked/。此外,文档根指令 (root) 的值不会改变。这个过程在动态脚本的眼中是看不见的。语法:目录(不要忘记结尾的 /)或文件路径

更新

@Richard Smith 建议将别名更改为 root 并将 C:/Server/apps/phpmyadmin/ 更改为 C:/Server/apps 并添加 ^~ 修饰符。现在我使用 root 而不是别名,我可以将 SCRIPT_FILENAME 备份到 $document_root$fastcgi_script_name;

【问题讨论】:

您可以尝试在别名末尾添加/ 吗?像这样:alias D:/Server/apps/phpmyadmin/;。另外,请报告当您使用脚本http://localhost/phpmyadmin/index.php 而不是仅http://localhost/phpmyadmin/ 明确请求 URL 时会发生什么,因为可能会出现第二个问题。 @blubberdiblub 在别名末尾添加 / 会给我同样的错误。如果我明确请求完整的 URL,它会给我一个 404 Not Found。我也尝试了许多其他组合。 【参考方案1】:

在您当前的配置中,任何带有.php 扩展名的文件都由相同的位置块处理,这对 laravel 来说很好,但对 phpmyadmin 来说是致命的。

您的phpmyadmin 位置块的优先级低于.php 位置块。看看如何nginxchooses a location to process a request。

您可以使用^~ 修饰符为phpmyadmin 位置块赋予比所有正则表达式位置块更高的优先级:

location ^~ /phpmyadmin/ 
    root D:/Server/apps;
    fastcgi_pass   php;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    ...

另外:rootis more efficient比aliasfastcgi_index只对$fastcgi_script_name起作用,所以你需要像上面那样改变SCRIPT_FILENAME

【讨论】:

感谢您的帮助!现在我有以下问题:当我访问 localhost/phpmyadmin/ (访问被拒绝),但 localhost/phpmyadmin/index.php 工作得很好! @Lucas 您需要更改 SCRIPT_FILENAME - 我在回答中添加了注释。 这是真的。现在我使用 root 而不是别名。再次感谢您拯救我的一天!

以上是关于Nginx 和 PHP-FPM - 权限被拒绝 - Windows的主要内容,如果未能解决你的问题,请参考以下文章

Apache FastCgi、PHP-FPM、Suexec 权限被拒绝错误

nginx 和 php-fpm 套接字所有者

使用 Nginx + PHP-FPM 拒绝访问 PHP 文件 (403)

nginx:无效选项:“off”和entrypoint.sh::权限被拒绝

Ubuntu上的nginx权限被拒绝

nginx 错误:(13:权限被拒绝)同时连接到上游)