Lithium用Nginx做前端代理后端Apache处理请求
最近项目里用到了一个php框架 Lithium http://lithify.me/ 官方只给了Apache的配置,在部署的时候发现用Nginx + PHP-fpm 总是有问题,折腾了一整天都没办法解决,
网上搜了半天,可能是php-fpm这类fcgi在处理一些参数时候的问题,比如说这个 php bug#55208 实在不行,只能换成Nginx做代理,缓存静态文件,其余的丢给后端apache去处理去。
nginx里的配置主要参考了 http://my.oschina.net/u/234530/blog/54310 的例子,做了一些修改,最重要的是try_files
server { listen [::]:80; ## listen for ipv4 #listen [::]:80 default ipv6only=on; ## listen for ipv6 server_name do.it; access_log off; error_log /var/log/nginx/doit.error.log; location / { root /opt/web/app/webroot; index index.php index.html; # Nginx找不到文件时,转发请求给后端Apache try_files $uri $uri/ @proxy; # css, js 静态文件设置有效期1天 location ~ ..(js|css)$ { access_log off; expires 1d; } # 图片设置有效期3天 location ~ ..(gif|jpg|jpeg|png|bmp|swf)$ { access_log off; expires 3d; } } # 动态文件.php请求转发给后端Apache location ~ .php$ { # 传递真实IP到后端 proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://127.0.0.1:8989; } location @proxy { proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://127.0.0.1:8989; } location ~ /.ht { deny all; } }apache里的配置就比较简单了,把端口改成8989,然后加入类似以下一段
<VirtualHost *:8989> ServerAdmin my@localhost DocumentRoot /opt/web/app/webroot <Directory /opt/web/app/webroot> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !favicon.ico$ RewriteRule ^ index.php [QSA,L] </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log # Possible values include: debug, info, notice, warn, error, crit, # alert, emerg. LogLevel warn CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>