Htaccess与Nginx的奇怪行为

我有一个运行在Nginx(v1.0.14)上的网站作为反向代理,代理请求到Apache(v2.2.19)。 所以Nginx在80端口上运行,Apache在8080上运行。

整体网站工作正常,但我不能阻止访问与.htaccess文件的某些目录。

例如,我有“我的保护目录”在“www.site.com”里面,我有以下代码的htaccess:

<Files *> order deny,allow deny from all allow from 1.2.3.4 <--- my ip address here </Files> 

当我尝试访问此网页与我的IP(1.2.3.4)我得到404错误,这不是我所期望的:

 http://www.site.com/my-protected-directory 

但是,当这个页面直接由Apache提供时,一切都按预期工作。 我可以看到这个页面,其他人都不能。

 http://www.site.com:8080/my-protected-directory 

更新。 Nginx的configuration(7.1.3.7是站点IP):

 user apache; worker_processes 4; error_log logs/error.log; pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; sendfile on; keepalive_timeout 65; gzip on; gzip_min_length 1024; gzip_http_version 1.1; gzip_proxied any; gzip_comp_level 5; gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript image/x-icon; server { listen 80; server_name www.site.com site.com 7.1.3.7; access_log logs/host.access.log main; # serve static files location ~* ^.+.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$ { root /var/www/vhosts/www.site.com/httpdocs; proxy_set_header Range ""; expires 30d; } # pass requests for dynamic content to Apache location / { proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $http_host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Range ""; proxy_pass http://7.1.3.7:8080; } } 

更新2 :mod_rpaf被安装并且ip地址被正确地确定

请谁能告诉我什么是错的,以及如何解决这个问题?

如果nginx代理到apache,那么与apache的连接来自nginx,而不是来自你,因此你的 IP永远不会进入方程式。

您可以根据原始IP(将存储在x-forwarded-for标头中)设置环境variables,然后允许使用该variables集的请求:

 <Location "/"> SetEnvIf X-Forwarded-For ^1\.2\.3\.4 proxy_env Order allow,deny Satisfy Any Allow from env=proxy_env </Location> 

我已经设法解决这个问题。 问题是在某些情况下Rpaf模块工作不正确。 “条件”我的意思是Apache版本和操作系统。 (我在CentOS上是Apache 2.2)

无论如何,要解决这个问题,你应该禁用rpaf模块并从这里安装补丁版本: mod_realip2

安装清晰简单。 希望这可以帮助一个人,因为我花了几个小时寻找解决scheme。

你可以尝试添加到nginx的服务器部分error_log <path> debug; ,运行该请求并查看它是如何针对位置规则进行分析的。 有时候不是那么明显

您将root指令放在location而不是server 。 这是最常见的nginx错误configuration之一 。 在这种情况下,它将直接负责你的404错误。