2017-05-28 115 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;」,但我不知道,因爲我從來沒有測試過這個配置。

不要認爲我的新位置順序(位置= /)與您的位置/位置相同,因爲我的順序等號僅適用於在沒有任何參數的情況下精確請求「主」位置。