如何使用Nginx对Laravel进行负载?
Laravel  /  管理员 发布于 7年前   195
项目环境php7.2, nginx , Laravel,开发的微信公众号应用 。目前访问量的上升,单台服务器不能满足需求,于是用nginx做了负载。
以下是一种可行性方案,目前正在使用。
session共享的问题参考:Laravel使用Redis共享Session(代码详解)
有两台web服务器 A:10.0.0.2
, B:10.0.0.3
,系统的域名为 www.c.com
,使用A使用为反向代理服务器
A服务器的nginx配置
server {listen 81;server_name _;index index.html index.htm index.php;access_log /data/wwwroot/wwwlogs/www_nginx.log combined;root /data/wwwroot/www/public;location / { try_files $uri $uri/ /index.php?$query_string;}location ~ .*\.(php|php5)?$ { fastcgi_pass unix:/dev/shm/php-cgi.sock; fastcgi_index index.php; include fastcgi.conf; }location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ { expires 30d; access_log off; }location ~ .*\.(js|css)?$ { expires 7d; access_log off; }}
B服务器nginx设置
server {listen 80;server_name -;index index.html index.htm index.php;access_log /data/wwwlogs/www_nginx.log combined;root /data/wwwroot/www/public;location / { try_files $uri $uri/ /index.php?$query_string;}location ~ .*\.(php|php5)?$ { fastcgi_pass unix:/dev/shm/php-cgi.sock; fastcgi_index index.php; include fastcgi.conf; }location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ { expires 30d; access_log off; }location ~ .*\.(js|css)?$ { expires 7d; access_log off; }}
A 服务器上的反向代理服务器设置
upstream backend{ ip_hash; server 127.0.0.1:81; server 10.0.0.3;}server {listen 80;server_name www.c.com;index index.html index.htm index.php;access_log /data/wwwroot/wwwlogs/www_nginx.log combined;root /data/wwwroot/www/public;location / { proxy_pass http://backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}location ~ .*\.(php|php5)?$ { fastcgi_pass unix:/dev/shm/php-cgi.sock; fastcgi_index index.php; include fastcgi.conf; }location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ { expires 30d; access_log off; }location ~ .*\.(js|css)?$ { expires 7d; access_log off; }}
如果是做微信公众号,使用了easywechat,需要把backend 代换为你的www.c.com 这样才能支持微信授权。
微信的access_token需要使用redis共享,easywechat默认使用laravel 的缓存,所以需要把.env的缓存改成使用redis
CACHE_DRIVER=redis
Laravel 中使用 Request::getClientIp()
获取到的IP不是真实的IP,需要修改app/Providers/AppServiceProvider.php
,设置信任代理服务器的IP(127.0.0.1,10.0.0.2),就可以使用Request::getClientIp()获真实IP了。
public function boot() { \Request::setTrustedProxies(['127.0.0.1','10.0.0.2']); }
更多laravel框架相关技术文章,请访问laravel教程栏目!
以上就是如何使用Nginx对Laravel进行负载?的详细内容,更多请关注其它相关文章!
123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..路人 在
php中使用hyperf框架调用讯飞星火大模型实现国内版chatgpt功能示例中评论 教程很详细,如果加个前端chatgpt对话页面就完美了..Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号