nginx+php 實現代理與負載均衡 (1臺nginx,2臺php)
實驗準備 3臺虛擬機器
192.168.239.136/192.168.239.140/192.168.239.144
1 安裝php 192.168.239.140/192.168.239.144
安裝之前先做好這些準備工作(安裝基礎環境)
yum -y groupinstall "Development Tools" "Development Libraries"
yum install -y epel-release
yum install -y libxml2-devel.x86_64 openssl-devel.x86_64 bzip2-devel.x86_64 libjpeg-turbo-devel.x86_64 libjpeg-turbo-static.x86_64 libpng-devel.x86_64 libpng-static.x86_64 freetype-devel.x86_64 libstdc++ libstdc++-devel compat-libstdc++-33 libstdc++-static gcc libmcrypt-devel lrzsz gcc gcc-c++ make man vim tree unzip wget curl lua-devel lua-static GeoIP GeoIP-devel patch libxml2-devel libxslt libxslt-devel gd gd-devel autoconf m4 pcre-devel.x86_64 pcre-static.x86_64 lrzsz ntp libcurl-devel.x86_64 libtool-ltdl-devel.x86_64
下載php原始碼包到 /usr/local/src cd /usr/local/src
解壓 cd 解壓目錄 useradd -s /sbin/nologin php-fpm
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=mysqlnd --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --enable-zend-multibyte --disable-ipv6 --with-pear --with-curl
make && make install
cp php.ini-development /usr/local/php-fpm/etc/php.ini 複製配置檔案
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm 複製啟動指令碼
mv /usr/local/php-fpm/etc/php-fpm.conf.default /usr/local/php-fpm/etc/php-fpm.conf
> /usr/local/php-fpm/etc/php-fpm.conf
vim !$ 加入如下配置
[global]
pid = /usr/local/php-fpm/var/run/php-fpm.pid
error_log = /usr/local/php-fpm/var/log/php-fpm.log
[www]
listen = 192.168.239.140:9000 (IP是php本機IP或者0.0.0.0,不能是127.0.0.1)
user = php-fpm
group = php-fpm
listen.owner = nobody
listen.group = nobody
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
slowlog = /usr/local/php-fpm/var/slow.log
request_slowlog_timeout = 1
php_admin_value[open_basedir]=/data/www/:/tmp/
/usr/local/php-fpm/sbin/php-fpm -t
chmod 755 /etc/init.d/php-fpm
/etc/init.d/php-fpm start netstat -lnpt 檢視是否有監聽 192.168.239.140:9000
mkdir -p /tmp/tmp
vim /tmp/tmp/index.php 建立動態測試檔案,寫入如下
dongtaiqingqiu 1
2.第二臺安裝php (安裝編譯都不變,配置檔案IP更改為 192.168.239.144:9000 最後index.php 中dongtaiqingqiu 1改為dongtaiqingqiu2 )
3.安裝nginx
下載原始碼包到 /usr/local/src cd /usr/local/src
wget
解壓 cd 解壓目錄
yum install -y pcre-devel
./configure --prefix=/usr/local/nginx --with-pcre
make
make install
4. 編寫nginx啟動指令碼
vim /etc/init.d/nginx 加入如下配置
#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"
start() {
echo -n $"Starting $prog: "
mkdir -p /dev/shm/nginx_temp
daemon $NGINX_SBIN -c $NGINX_CONF
RETVAL=$?
echo
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -TERM
rm -rf /dev/shm/nginx_temp
RETVAL=$?
echo
return $RETVAL
}
reload(){
echo -n $"Reloading $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -HUP
RETVAL=$?
echo
return $RETVAL
}
restart(){
stop
start
}
configtest(){
$NGINX_SBIN -c $NGINX_CONF -t
return 0
}
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
configtest)
configtest
;;
*)
echo $"Usage: $0 {start|stop|reload|restart|configtest}"
RETVAL=1
esac
exit $RETVAL
儲存後,執行
chmod a+x /etc/init.d/nginx 或者 chmod 755 !$ 修改許可權
service nginx start 啟動nginx
5. 配置反向代理
> /usr/local/nginx/conf/nginx.conf
vim /usr/local/nginx/conf/nginx.conf 加入如下配置
user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 6000;
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 3526;
server_names_hash_max_size 4096;
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
'$host "$request_uri" $status'
'"$http_referer" "$http_user_agent"';
sendfile on;
tcp_nopush on;
keepalive_timeout 30;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
connection_pool_size 256;
client_header_buffer_size 1k;
large_client_header_buffers 8 4k;
request_pool_size 4k;
output_buffers 4 32k;
postpone_output 1460;
client_max_body_size 10m;
client_body_buffer_size 256k;
client_body_temp_path /usr/local/nginx/client_body_temp;
proxy_temp_path /usr/local/nginx/proxy_temp;
fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
fastcgi_intercept_errors on;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_comp_level 5;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css text/htm application/xml;
include vhosts/*.conf;
}
配置代理
mkdir /usr/local/nginx/conf/vhosts
vi /usr/local/nginx/conf/vhosts/php.conf 加入如下配置
upstream mysvr {
#weigth參數列示權值,權值越高被分配到的機率越大
server 192.168.239.144:9000 weight=2;
server 192.168.239.140:9000 weight=1;
}
server
{
listen 80;
server_name
index index.html index.htm index.php;
root /usr/local/nginx/html;
location ~ .php$ {
include fastcgi_params;
fastcgi_pass mysvr;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /tmp/tmp$fastcgi_script_name;
}
}
/usr/local/nginx/sbin/nginx -t 檢測配置檔案
/usr/local/nginx/sbin/nginx -s reload (直接重新載入配置,不需要重啟)
vim /usr/local/nginx/html/1.html 建立靜態測試檔案,寫入
jingtaifangwen
設定本地解析
修改windons hosts(地址 c:windowssystem32driversetchosts)記事本開啟,最下面新增一行配置
192.168.239.136
測試:最好用谷歌瀏覽器 (如下顯示 代理成功)
訪問 顯示的是nginx 預設頁
訪問 /.html 顯示的jingtaifangwen
訪問 /index.php 多重新整理幾次 會顯示 dongtaifangwen1 一次 dongtaifangwen2 兩次
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/3705/viewspace-2802998/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- LNMP 分散式叢集(一):Nginx+PHP平臺搭建與負載均衡配置LNMP分散式NginxPHP負載
- nginx實現兩臺服務負載均衡Nginx負載
- 【Nginx】Windows平臺下配置Nginx服務實現負載均衡NginxWindows負載
- 一臺伺服器實現nginx代理負載均衡同時使用web服務伺服器Nginx負載Web
- nginx+tomcat實現反向代理+負載均衡NginxTomcat負載
- nginx實現負載均衡Nginx負載
- Nginx代理功能與負載均衡詳解Nginx負載
- nginx面試題-nginx負載均衡與正反向代理Nginx面試題負載
- Nginx入門(2)反向代理和負載均衡Nginx負載
- Nginx + IIS 實現負載均衡Nginx負載
- nginx+php負載均衡叢集環境中的session共享方案梳理NginxPHP負載Session
- 介紹下Nginx 反向代理與負載均衡Nginx負載
- 【Nginx】Nginx反向代理和負載均衡部署Nginx負載
- Nginx反向代理+負載均衡簡單實現(https方式)Nginx負載HTTP
- Nginx 兩臺伺服器配置負載均衡!!!Nginx伺服器負載
- nginx詳解反向代理負載均衡Nginx負載
- nginx反向代理和負載均衡策略實戰案例Nginx負載
- Nginx 動靜分離與負載均衡的實現Nginx負載
- Nginx伺服器的使用與反向代理負載均衡Nginx伺服器負載
- Nginx如何實現四層負載均衡?Nginx負載
- nginx+tomcat實現負載均衡NginxTomcat負載
- Nginx實現簡單的負載均衡Nginx負載
- nginx反向代理負載均衡帶你突破單臺伺服器的瓶頸Nginx負載伺服器
- Nginx負載均衡反向代理伺服器Nginx負載伺服器
- [原創]Nginx反向代理及負載均衡Nginx負載
- nginx Win下實現簡單的負載均衡(1)nginx搭建部署Nginx負載
- nginx反向代理負載均衡與動靜頁面分離Nginx負載
- 【nginx】用Redware、Nginx、Keepalive實現的負載均衡Nginx負載
- Keepalived實現Nginx負載均衡高可用Nginx負載
- Docker Compose+nginx實現負載均衡DockerNginx負載
- 用Nginx實現Session共享的均衡負載NginxSession負載
- Nginx如何實現負載均衡釋出策略?Nginx負載
- NGINX 負載均衡Nginx負載
- 【Nginx】負載均衡Nginx負載
- nginx負載均衡Nginx負載
- Nginx反向代理負載均衡的容器化部署Nginx負載
- docker下nginx反向代理和負載均衡配置DockerNginx負載
- nginx+tomcat反向代理負載均衡配置NginxTomcat負載