centos安裝nginx 帶upstream
用途:利用upstream進行socket數據中轉
各版本nginx下載地址:http://nginx.org/download/
系統:CentOS 6.5 x64
nginx版本:1.12.1/1.15.10
安裝方式:源碼編譯安裝
1.安裝必須環境,nginx的編譯需要c++,同時prce(重定向支持)和openssl(https支持)也需要安裝
yum install gcc-c++ yum -y install pcre* yum -y install openssl*
2.下載nginx-1.12.1.tar.gz,放在 /usr/local/ 目錄下
cd /usr/local/ wget http://nginx.org/download/nginx-1.12.1.tar.gz tar zxf nginx-1.12.1.tar.gz cd nginx-1.12.1 ./configure --prefix=/usr/local/nginx --with-stream make && make install
參考:https://www.cnblogs.com/chenjianxiang/p/8489055.html
3.打開防火牆需要允許訪問的端口,如端口80,或者直接關閉防火牆
[root@localhost ~]#servcie iptables stop //臨時關閉防火牆 [root@localhost ~]#chkconfig iptables off //永久關閉防火牆
注:如果提示service not found,下面三種方式解決
a:yum install initscripts -y
b:直接使用su - root來切換到root用戶,然后使用 service
c:使用su root切換到root用戶,並同時使用/sbin/service來操作,/sbin/service iptables stop
4.nginx.conf配置文件如下
user nobody; worker_processes 1; error_log logs/error.log; error_log logs/error.log notic/sbine; error_log logs/error.log info; pid logs/nginx.pid; events { worker_connections 1024; } stream { upstream upstream_8002 { server us-free.hyss.xyz:48528; } server { listen 8002; listen 8002 udp; proxy_pass upstream_8002; proxy_timeout 10s; proxy_connect_timeout 10s; } }
5.進入/usr/local/nginx 啟動nginx:./nginx
6.如果端口被占用
查找端口占用
netstat -lnp|grep xxx xxx請換為你的nginx需要的端口,如:80,會提示進程多少如:1777
ps 1777 可以看到是哪個路徑
kill -9 1777 //殺掉編號為1777的進程
7.nginx啟動報錯
/usr/local/nginx/logs/nginx.pid 路徑下找不到nginx.pid
執行:/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
8:centos7 設置開機啟動 vim /etc/rc.d/rc.local
增加內容
/usr/local/nginx/sbin/nginx -c /nginx/nginx-1.15.10/conf/nginx.conf
注意 -c 之后的 文件.. 需要確定.
9:centos7關閉防火牆 1、firewalld的基本使用
啟動: systemctl start firewalld
關閉: systemctl stop firewalld
查看狀態: systemctl status firewalld
開機禁用 : systemctl disable firewalld
開機啟用 : systemctl enable firewalld