首页 >后端开发 >php教程 >nginx作为php站点的负载均衡实践

nginx作为php站点的负载均衡实践

WBOY
WBOY原创
2016-08-08 09:28:171115浏览

我负责的一个lamp应用,需要提高高可用性,所以选择nginx作为负载均衡的工具。
nginx需要修改一下nginx.conf配置:

<code>upstream qss {
     <span>82</span>         server <span>10.46</span><span>.194</span><span>.17</span>:<span>8088</span> weight<span>=</span><span>5</span>;
     <span>83</span>         server <span>10.46</span><span>.192</span><span>.41</span>:<span>8080</span> weight<span>=</span><span>5</span>;
     <span>84</span>     }
     <span>85</span><span>86</span>     server {
     <span>87</span>         listen <span>8079</span>;
     <span>88</span>         server_name cq01<span>-tdw</span><span>-bfe28</span><span>.</span>cq01<span>.</span>baidu<span>.</span>com;
     <span>89</span>         underscores_in_headers <span>on</span>;
     <span>90</span>         ignore_invalid_headers off;
     <span>91</span><span>92</span>         location <span>/</span> {
     <span>93</span>             proxy_set_header Host <span>$host</span>;
     <span>94</span>             proxy_set_header X<span>-Real</span><span>-IP</span><span>$remote_addr</span>;
     <span>95</span>             proxy_set_header X<span>-Forwarded</span><span>-For</span><span>$proxy_add_x_forwarded_for</span>;
     <span>96</span>             proxy_pass http:<span>//qss;</span><span>97</span>         }</code>

其中,underscores_in_headers on的意思是包含下划线的header字段被认为是合法的,如果header中有下划线字段,一定要设置这个,否则会在nginx的error_log中提示错误。

另外,站点是有session的,所以各个php模块中应该将session共享起来,可以修改php.ini文件的配置,之前是保存在tmp的文件中,现在,可以保存在memcached中:

<code><span>732</span> extension_dir = <span>"/home/qec/vlamp/build/php/output/lib/php/extensions/no-debug-non-zts-20100525/"</span><span>733</span> extension = memcache<span>.so</span><span>1365</span> [Session]
   <span>1366</span><span>; Handler used to store/retrieve data.</span><span>1367</span><span>; http://php.net/session.save-handler</span><span>1368</span><span>; session.save_handler = files</span><span>1369</span> session<span>.save</span>_handler = memcache
   <span>1370</span> memcache<span>.hash</span>_strategy = <span>"consistent"</span><span>1371</span> session<span>.save</span>_path = <span>"tcp://10.216.122.21:11211"</span></code>

然后,就可以了。

以上就介绍了nginx作为php站点的负载均衡实践,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn