1.下載源碼
wget http://nginx.org/download/nginx-1.12.0.tar.gz
2.編譯前配置
解壓之後,進入源碼目錄,進行編譯前配置,執行:
./configure --with-stream --user=nginx --group=nginx --prefix=/application/nginx-1.12.0/ --with-http_v2_module --with-http_ssl_module --with-http_sub_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre
3.編譯並安裝
make && make install
4.創建軟鏈
ln -s /application/nginx-1.12.0/ /application/nginx
ln -s /application/nginx/sbin/* /usr/local/sbin/
5.配置服務
cd /application/nginx
vim conf/nginx.conf
刪除http模塊,添加以下內容,並按照提示根據自己的業務場景修改相應的ip和端口。
stream {
# 第一組代理服務:
# Nginx的3333端口--->192.168.1.184:3333
# Nginx的3333端口--->192.168.1.26:3333
server {
listen 3333; #監聽端口3333,即代理服務器的端口,不是真正提供服務的端口。
proxy_pass proxy_3333; #轉發請求
}
upstream proxy_3333 {
# simple round-robin 轉發IP和端口
server 192.168.1.184:3333;
server 192.168.1.26:3333;
}
# 第二組代理服務:
# Nginx的3334端口--->192.168.1.184:3334
# Nginx的3334端口--->192.168.1.26:3334
server {
listen 3334; #監聽端口3334
proxy_pass proxy_3334; #轉發請求
}
upstream proxy_3334 {
# simple round-robin 轉發IP和端口
server 192.168.1.184:3334;
server 192.168.1.26:3334;
}
}
6.常用命令
檢查配置文件:nginx -t
啓動nginx:nginx
停止nginx:nginx -s stop
重啓nginx:nginx -s reload
指定其他配置文件啓動:nginx -c /application/nginx/conf/nginx.conf.bak