2017-05-28 44 views
1

我已经成功安装了使用PHP-FPM的nginx,但不幸的是,当从不同目录加载我的php文件时遇到了一些麻烦。我所有的文件都位于/ var/www/html的子目录中(例如,所有的css文件都位于/ var/www/html/css中,所有的javascript文件都位于/ var/www/html/js中,所有的php - 文件位于/ var/www/html/php)。 根据这个,我改变了根目录路径为我的PHP文件到/ var/www/html等/ PHP:nginx:403在单独目录下的php文件被禁止

server { 
    listen 80 default_server; 
    listen [::]:80 default_server; 

    # SSL configuration 
    # 
    # listen 443 ssl default_server; 
    # listen [::]:443 ssl default_server; 
    # 
    # Note: You should disable gzip for SSL traffic. 
    # See: https://bugs.debian.org/773332 
    # 
    # Read up on ssl_ciphers to ensure a secure configuration. 
    # See: https://bugs.debian.org/765782 
    # 
    # Self signed certs generated by the ssl-cert package 
    # Don't use them in a production server! 
    # 
    # include snippets/snakeoil.conf; 

    root /var/www/html; 

    # Add index.php to the list if you are using PHP 
    index index.html index.htm index.php home.php; 

    server_name _; 

    location/{ 
      # First attempt to serve request as file, then 
      # as directory, then fall back to displaying a 404. 
      try_files $uri $uri/ =404; 
    } 

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 
    # 
    location ~ \.php$ { 
      root /var/www/html/php; 

      include snippets/fastcgi-php.conf; 

    #  # With php7.0-cgi alone: 
    #  fastcgi_pass 127.0.0.1:9000; 
      # With php7.0-fpm: 
      fastcgi_pass unix:/run/php/php7.0-fpm.sock; 
    } 
} 

不幸的是,使用我的Web浏览器访问我的nginx服务器时,我收到错误403 (禁止)。当直接访问我的index.php(http://192.168.2.109/index.php)时,一切正常。所以,我认为这意味着文件许可权是正确的,但nginx无法索引/ var/www/html/php目录。此外,/var/log/nginx/error.log包括:

2017/05/28 07:49:56 [error] 13678#13678:* 1目录索引“/ var/www/html /”是禁,客户端:192.168.2.101,服务器:_,请求: “GET/HTTP/1.1”,主持人:

我已经尝试过让自动索引并添加指数符在 “192.168.2.109” “location〜.php $ {”部分没有成功。其结果是一样的:(

有没有人有一个想法,我在做什么在Nginx 403 error: directory index of [folder] is forbidden错误/缺少在这里所有的建议并没有解决我的问题

+0

很确定你只需要一个默认索引? https://stackoverflow.com/questions/10002439/make-index-html-default-but-allow-index-php-to-be-visited-if-typed-in – Joe

+0

nginx是否有一个“DefaultIndex”指令配置文件?提供的链接显示了htaccess的指令,但我没有使用htaccess。 – Nrgyzer

+0

它不会是一个nginx的东西,它将是一个Web服务器配置的事情。我不知道如何处理所有可能的服务器设置。我知道如果你没有在URL中指定一个索引,那么.htaccess中的DirectoryIndex声明就可以工作,但我不知道如何让Web服务器自动完成。我知道服务器可以被设置为为目录遍历抛出一个禁止的错误,这就是为什么很多网站在他们不希望人们玩耍的目录中只有一个空白的index.html。 – Joe

回答

0

的问题很简单:?

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 
    # 
    location ~ \.php$ { 
      root /var/www/html/php; 

      include snippets/fastcgi-php.conf; 

    #  # With php7.0-cgi alone: 
    #  fastcgi_pass 127.0.0.1:9000; 
      # With php7.0-fpm: 
      fastcgi_pass unix:/run/php/php7.0-fpm.sock; 
    } 

正如我可以读,当你请求一个*。PHP文件,你改变了根地址。但是,当你请求“默认索引”,您请求/,不是以.php

你需要一个新的位置,以便根据/请求更改根路径。尝试使用以下内容:

location =/{ 
      root /var/www/html/php; 

      include snippets/fastcgi-php.conf; 

      fastcgi_pass unix:/run/php/php7.0-fpm.sock; 
    } 

也许,在这个配置中,你需要把一个“rewrite index.php;”,但我不知道,因为我从来没有测试过这个配置。

不要认为我的新位置顺序(位置= /)与您的位置/位置相同,因为我的顺序等号仅适用于在没有任何参数的情况下精确请求“主”位置。