apache httpd服務器403 forbidden的問題


一、問題描述

在apache2的httpd配置中,很多情況都會出現403。

剛安裝好httpd服務,當然是不會有403的問題了。主要是修改了一些配置后出現,問題描述如下:

  • 修改了DocumentRoot目錄指向后,站點出現403錯誤。
  • 設置了虛擬主機目錄也可能導致403。
  • apache的httpd服務成功啟動,看起來都很正常,卻沒有權限訪問
  • 日志出現: access to / denied (filesystem path '/srv/lxyproject/wsgi/django.wsgi') because search permissions are missing on a component of the path
  • 設置虛擬目錄后,錯誤日志出現:client denied by server configuration: /srv/lxyproject/wsgi/django.wsgi

二、分析問題及方案

下面一步步解決問題時注意錯誤日志內容。ok,開始。

1、httpd.conf中目錄配置文件

如果顯示更改了DocumentRoot,比如改為 "/usr/local/site/test" 。site目錄和test目錄是通過使用mkdir建立的,然后在test目錄下放一個index.html。這種情況應該查看httpd.conf中配置。

你的<Directory "/usr/local/site/test">一定要和DocumentRoot一致,因為這段Directory是apache對該目錄訪問權限的設置,只有設置正確的目錄,DocumentRoot才會生效。

<Directory "/usr/local/site/test">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
    # for more information.
    #
    Options Indexes FollowSymLinks

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    AllowOverride None

    #
    # Controls who can get stuff from this server.
    #
    Require all granted
</Directory>

2、目錄訪問權限

第一步配置正確還是出現403,檢查目錄配置<Directory "/usr/local/site/test">中是否有Deny from all。有則所有訪問都會被拒絕,當然403了。

可以設置為Allow from all或者Require all granted來處理。

不要修改<Directory />根目錄中Deny from all。

3、目錄權限

如果至此還是403,可能是網站目錄的權限問題。

apache要求目錄具有執行權限,也就是x,要注意的是,你的目錄樹都應該擁有這些權限。

假如你的目錄是/usr/local/site/test,那么要保證/usr,/usr/local,/usr/local/site,/usr/local/site/test這四個層級的目錄都是755權限。

#chmod 755 /usr/local/site
#chmod 755 /usr/local/site/test

我犯過一個錯就是只設置了最后一級目錄權限,沒有設置上級目錄權限,導致403。

4、 虛擬目錄

【這個問題我沒遇到過,因為我沒這樣寫過,網上資料這么寫,可作為參考】

如果設置的是虛擬目錄,那么你需要在httpd.conf中定義一個虛擬目錄,而且像極了如下的片段:

Alias /folder "/usr/local/folder"                       

<Directory "/usr/local/folder">                         
    Options FollowSymLinks                            
    AllowOverride None                              
    Order deny,allow                               
    Deny from all                                 
    Allow from 127.0.0.1 192.168.1.1                       
</Directory>      

如果是這一種情況,而且你寫得類似我上面的代碼,三處folder都是一樣一樣的,那絕對會是403!怎么解決呢,就是把跟在Alias后面斜杠后面的那串改了,改成什么,不要和虛擬目錄的文件夾同名就好,然后我就可以用改過后的虛擬目錄訪問了,當然,改文件夾也行,只要你不怕麻煩,只要Alias后面的虛擬目錄定義字符(紅色)和實際文件夾名(黑色)不相同就OK。

5、selinux的問題

如果依然是403,那就是selinux在作怪了,於是,你可以把你的目錄進行一下selinux權限設置。

今天我遇到的就是這個問題了。

#chcon -R -t httpd_sys_content_t /usr/local/site
#chcon -R -t httpd_sys_content_t /usr/local/site/test

網上資料說不過,這一步大多不會發生。但我的問題確實是,可能跟系統有關,具體原理還不是很懂。

6、wsgi的問題

我的虛擬主機配置為:

<VirtualHost *:80>

WSGIScriptAlias / /srv/lxyproject/wsgi/django.wsgi
Alias /static/ /srv/lxyproject/collectedstatic/

ServerName 10.1.101.31
#ServerName example.com
#ServerAlias www.example.com

<Directory /srv/lxyproject/collectedstatic>
  Options Indexes  FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

<Directory /srv/lxyproject/wsgi/>
    Allow from all
</Directory>
ErrorLog   /etc/httpd/logs/lxyproject.error.log
LogLevel warn
</VirtualHost>

我訪問

log報錯:

client denied by server configuration: /srv/lxyproject/wsgi/django.wsgi

解決辦法:

修改<Directory /srv/lxyproject/wsgi/>中Allow from all為:Require all granted

這個問題是因為版本的原因,

我的httpd版本為:

[root@yl-web conf.d]# rpm -qa |grep httpd
httpd-devel-2.4.6-31.el7.centos.x86_64
httpd-tools-2.4.6-31.el7.centos.x86_64
httpd-2.4.6-31.el7.centos.x86_64

而2.3以下版本用Allow from all,2.3及以上版本為Require all granted。

<Directory /home/aettool/aet/apache>
  <IfVersion < 2.3 >
   Order allow,deny
   Allow from all
  </IfVersion>
  <IfVersion >= 2.3>
   Require all granted
  </IfVersion>
</Directory>

 參考:http://stackoverflow.com/questions/17766595/403-forbidden-error-with-django-and-mod-wsgi

 

本文作者starof,因知識本身在變化,作者也在不斷學習成長,文章內容也不定時更新,為避免誤導讀者,方便追根溯源,請諸位轉載注明出處:http://www.cnblogs.com/starof/p/4685999.html 有問題歡迎與我討論,共同進步。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM