我正在尝试使用Nginx作为运行PHP站点的Apache的caching反向代理。 这是一种工作,但试图重写Apache设置的URL时,问题来了,它不工作,因为我希望。 这是设置。
我有一个基于Joomla的PHP网站在Apache上运行,端口8080(http)和44343(https) – 这工作正常,这里没有问题。 我已经删除了Joomla .htaccess文件,因为我认为Nginx会重写index.php本身,并照顾caching必要的PHP文件。
Joomla重写URL如下example.com/page1 => example.com/index.php/page1
我有Nginx监听端口80,443。下面是网站configuration
server { listen 80; listen [::]:80; server_name www.example.com return 301 https://$server_name$request_uri; } server { client_max_body_size 100M; autoindex off; listen 443 ssl; listen [::]:443 ssl; server_name www.example.com; server_name_in_redirect off; ssl_certificate /example.com/ssl.crt; ssl_certificate_key /example.com/ssl.key; root /var/www/site; index index.php index.html index.htm default.html default.htm; location / { try_files $uri $uri/ /index.php$uri; } # Reverse Proxy and Proxy Cache Configuration location ~ \.php { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header Host $host; proxy_pass https://127.0.0.1:44343; # Cache configuration proxy_cache reverse_cache; proxy_cache_valid 3s; proxy_no_cache $cookie_PHPSESSID; proxy_cache_bypass $cookie_PHPSESSID; proxy_cache_key "$scheme$host$request_uri"; add_header X-Cache $upstream_cache_status; } ... }
所以这加载网站,但无论我去哪个网页,它总是加载主页。 它似乎没有像在try_files部分预期的那样将$uri发送到index.php 。
我可以做这个工作的唯一方法是通过以下方式修改它:
#location / { # try_files $uri $uri/ /index.php$uri; #} # Reverse Proxy and Proxy Cache Configuration location / { ... }
这只是将整个请求发送到Apache,我必须带回.htaccess文件,它允许Apache进行重写。
该网站按预期加载,但问题是没有PHP文件被caching(如在/ var / cache中检查)。 所以Nginx必须重写才能知道哪个PHP文件被caching了哪个请求。
任何想法我怎么能做到这一点?