我負責的一個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教學有興趣的朋友有幫助。