Linux下安装使用Nginx
安装
直接从官网下载压缩包,nginx-1.15.9.tar.gz
解压后,在解压目录进行安装
安装命令:./configure && make && make install
./configure --help 查看能安装的模块,/–with"表示启用模块
./configure --with-stream 【支持TCP/UDP负载均衡】
详情见:https://blog.csdn.net/DMDD_NB/article/details/88800173
安装后执行: ./configure --with-stream && make && make install
安装完成。
安装报错:
./configure: error: the HTTP rewrite module requires the PCRE library.
安装pcre-devel解决问题:
yum -y install pcre-devel
./configure: error: the HTTP gzip module requires the zlib library.
安装zlib-devel解决问题:
yum install -y zlib-devel
启动Nginx:
进入nginx安装后的目录:usr/local/nginx/sbin
检查nginx.conf配置的准确性:./nginx -t
启动执行命令:./nginx -c /usr/local/nginx/conf/nginx.conf 或者直接 ./nginx
若是80的配置没有改,可以访问:localhost:80 检查启动是否成功
Log文件夹里也有日志可以查看。
其他几个命令:
./nginx -s reload # 重新载入配置文件
./nginx -s reopen # 重启 Nginx
./nginx -s stop # 停止 Nginx
重启的另一个方法:
查找当前nginx进程号,然后输入命令:kill -HUP 进程号 实现重启nginx服务
启动后可以打开/usr/local/nginx/logs/error.log或者acces.log实时查看监听的端口情况
坑1:connect() failed (111: Connection refused) while connecting to upstream
仔细检查这个端口启动情况,和转发的端口启动情况,要监听和转发的端口都启动成功才不会报错。。我的报错就是这么解决的。
nginx.config 配置稍稍解释
http 负载均衡例子
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
gzip_comp_level 9;
gzip_types text/css text/xml application/javascript;
gzip_vary on;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# 后端指向 server 的 9000端口 cns_node 组
upstream cns_node {
server 10.50.2.51:8585 weight=3;
server 10.50.2.59:8585 weight=3;
}
server {
listen 9000;
server_name umout.com;
access_log /etc/nginx/logs/server02_access.log main;
location /{
index index.html index.htm index.jsp;
proxy_pass http://cns_node1;
include /etc/nginx/proxy.conf;
}
}
}
TCP负载均衡:
stream {
upstream demo{
server 127.0.0.1:8089; # 这里配置成要访问的地址
server 127.0.0.2:1935;
server 127.0.0.3:1935; #需要代理的端口
}
server {
listen 1935; # 需要监听的端口
proxy_timeout 20s;
proxy_pass demo;
}
upstream demo2{
server 127.0.0.1:9100; # 这里配置成要访问的地址
server 127.0.0.3:9100; #需要代理的端口
}
server {
listen 9000; # 需要监听的端口
proxy_timeout 20s;
proxy_pass demo2;
}
}