Nginx是俄罗斯人Igor Sysoev编写的轻量级Web服务器,它的发音为 [ˈendʒɪnks] ,它不仅是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/SMTP 代理服务器。
Nginx以事件驱动的方式编写,所以有非常好的性能,同时也是一个非常高效的反向代理、负载平衡服务器。在性能上,Nginx占用很少的系统资源,能支持更多的并发连接,达到更高的访问效率;在功能上,Nginx是优秀的代理服务器和负载均衡服务器;在安装配置上,Nginx安装简单、配置灵活。
Nginx支持热部署,启动速度特别快,还可以在不间断服务的情况下对软件版本或配置进行升级,即使运行数月也无需重新启动。
在微服务的体系之下,Nginx正在被越来越多的项目采用作为网关来使用,配合Lua做限流、熔断等控制。
对于大多数使用者来说,Nginx只是一个静态文件服务器或者http请求转发器,它可以把静态文件的请求直接返回静态文件资源,把动态文件的请求转发给后台的处理程序,例如JAVA、php-fpm、apache、tomcat、jetty等,这些后台服务,即使没有nginx的情况下也是可以直接访问的(有些时候这些服务器是放在防火墙的面,不是直接对外暴露,通过nginx做了转换)。
Nginx的优点:
[root@dxm28 /]# dnf -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
[root@dxm28 /]# mkdir -p /data/nginx
[root@dxm28 /]# cd /data/nginx/
[root@dxm28 nginx]# wget https://mirrors.huaweicloud.com/nginx/nginx-1.19.6.tar.gz
[root@dxm28 nginx]# ll -h
[root@dxm28 nginx]# tar -zxvf nginx-1.19.6.tar.gz
使用./configure --help查看各个模块的使用情况。
–without-http_ssi_module的方式关闭不需要的模块
–with-http_perl_modules方式安装需要的模块
–prefix 指定安装路径
–with-http_stub_status_module允许查看nginx状态的模块
–with-http_ssl_module支持https的模块
[root@dxm28 nginx]# cd nginx-1.19.6/
[root@dxm28 nginx-1.19.6]# ./configure --prefix=/usr/local/nginx --user=root --group=root --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_sub_module --with-http_gzip_static_module --with-pcre
[root@dxm28 nginx-1.19.6]# make && make install
[root@dxm28 nginx-1.19.6]# /usr/local/nginx/sbin/nginx -t
#先检查用户nginx和组nginx是否存在
#检查组是否存在
cat /etc/group | grep nginx
#如果不存在,创建组
groupadd nginx
#检查用户nginx是否存在
cat /etc/passwd | grep nginx
或者
id nginx
#如果不存在,创建用户
useradd -g nginx nginx
[root@dxm28 nginx-1.19.6]# chown -R nginx.nginx /usr/local/nginx
[root@dxm28 nginx-1.19.6]# su nginx
[nginx@dxm28 nginx-1.19.6]$ /usr/local/nginx/sbin/nginx
问题:nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)
原因:Linux只有root用户可以使用1024以下的端口。
解决办法:
[nginx@dxm28 nginx-1.19.6]$ netstat -lntup |grep nginx
[nginx@dxm28 nginx-1.19.6]$ ps aux | grep nginx
[nginx@dxm28 nginx-1.19.6]$ curl http://localhost:8082
看到Welcome to nginx!,说明启动成功了。我们浏览器访问一下:
也可以使用root用户启动。后面我们还是改用root用户启动,端口还是80。
./nginx -s quit: (温和)较stop相比就比较温和一些了,此方式停止步骤是待nginx进程处理任务完毕进行停止。
./nginx -s stop: (强硬)此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程,这种方法比较强硬,无论进程是否在工作,都直接停止进程。
./nginx -s reload 重启nginx(不推荐此方法,推荐先停止在启动)
./nginx -s reopen reopening the log files 用来打开日志文件,这样nginx会把新日志信息写入这个新的文件中
创建nginx.service服务文件
[root@dxm28 nginx-1.19.6]# vim /etc/systemd/system/nginx.service
内容如下:
[Unit]
Description=nginx server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
User=root
Group=root
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@dxm28 nginx-1.19.6]# systemctl daemon-reload
# 停止上面手动后台启动的nginx服务
[root@dxm28 nginx-1.19.6]# ps aux | grep nginx
root 73601 0.0 0.0 24420 820 ? Ss 18:27 0:00 nginx: master process /usr/local/nginx/sbin/nginx
root 73602 0.0 0.0 24788 2528 ? S 18:27 0:00 nginx: worker process
root 75427 0.0 0.0 12320 1076 pts/0 S+ 21:43 0:00 grep --color=auto nginx
[root@dxm28 nginx-1.19.6]$ /usr/local/nginx/sbin/nginx -s quit
# 检查是否还存在nginx进程
[root@dxm28 nginx-1.19.6]# ps aux | grep nginx
root 75515 0.0 0.0 12320 1092 pts/0 S+ 21:48 0:00 grep --color=auto nginx
# 查询所有服务单元是否有nginx
[root@dxm28 nginx-1.19.6]# systemctl list-unit-files | grep nginx
nginx.service disabled
# 存在,且非开启自启动,使用systemctl启动nginx服务
[root@dxm28 nginx-1.19.6]# systemctl start nginx.service
# 查看nginx服务状态
[root@dxm28 nginx-1.19.6]# systemctl status nginx.service
Active: active (running) 可以看到nginx服务已经启动成功。查看进程以及端口的监听情况:
[root@dxm28 logs]# ps aux | grep nginx
[root@dxm28 logs]# netstat -ntlp | grep 80
[root@dxm28 nginx-1.19.6]# systemctl list-unit-files | grep nginx
nginx.service disabled
# disabled表示非开机自启动
# 设置为开机自启动
[root@dxm28 nginx-1.19.6]# systemctl enable nginx.service
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /etc/systemd/system/nginx.service.
# 再次查看
[root@dxm28 nginx-1.19.6]# systemctl list-unit-files | grep nginx
nginx.service enabled
# enabled表示是开机自启动,执行重启命令
[root@dxm28 nginx-1.19.6]# reboot
#重启后再次查看nginx服务已启动完成。
基于前几篇文章,我们已经搭建好了prometheus+node_exporter+grafana、redis集群、elk、rocketmq集群、tomcat等服务,那么我们接下来就抽取其中的几个服务作为例子来配置一下反向代理吧。
修改/etc/hosts配置文件加入promethues、grafana、elasticsearch、logstash、kibana、tomcat、rocketmq控制台的host映射。如下:
#配置nginx反向代理开始
192.168.0.28 promethues.dxm.com
192.168.0.28 grafana.dxm.com
192.168.0.28 elasticsearch.dxm.com
192.168.0.28 logstash.dxm.com
192.168.0.28 kibana.dxm.com
192.168.0.28 tomcat.dxm.com
192.168.0.28 rocketmqconsole.dxm.com
#配置nginx反向代理结束
首先在主配置文件(nginx.conf)中添加一行代码:
# 将这些代码写在http模块中
include prometheus.conf;
include grafana.conf;
include elasticsearch.conf;
include logstash.conf;
include kibana.conf;
include tomcat.conf;
include rocketmqconsole.conf;
注意位置千万不要写错,否则配置不生效。
配置文件的名字一定要写*.conf 如:promethues.conf grafana.conf
在/usr/local/nginx/conf/目录下新增加一个prometheus.conf配置文件,server配置如下:
server {
listen 80;
server_name promethues.dxm.com;
location / {
proxy_pass http://192.168.0.22:9090;
index index.html index.htm index.jsp;
}
}
在/usr/local/nginx/conf/目录下新增加一个grafana.conf配置文件,server配置如下:
server {
listen 80;
server_name grafana.dxm.com;
location / {
proxy_pass http://192.168.0.22:3000;
index index.html index.htm index.jsp;
}
}
在/usr/local/nginx/conf/目录下新增加一个elasticsearch.conf配置文件,server配置如下:
server {
listen 80;
server_name elasticsearch.dxm.com;
location / {
proxy_pass http://192.168.0.27:9200;
index index.html index.htm index.jsp;
}
}
在/usr/local/nginx/conf/目录下新增加一个logstash.conf配置文件,server配置如下:
server {
listen 80;
server_name logstash.dxm.com;
location / {
proxy_pass http://192.168.0.27:9200;
index index.html index.htm index.jsp;
}
}
在/usr/local/nginx/conf/目录下新增加一个kibana.conf配置文件,server配置如下:
server {
listen 80;
server_name kibana.dxm.com;
location / {
proxy_pass http://192.168.0.27:5601;
index index.html index.htm index.jsp;
}
}
在/usr/local/nginx/conf/目录下新增加一个tomcat.conf配置文件,server配置如下:
server {
listen 80;
server_name tomcat.dxm.com;
location / {
proxy_pass http://127.0.0.1:8080;
index index.html index.htm index.jsp;
}
}
在/usr/local/nginx/conf/目录下新增加一个rocketmqconsole.conf配置文件,server配置如下:
server {
listen 80;
server_name rocketmqconsole.dxm.com;
location / {
proxy_pass http://192.168.0.27:8080;
index index.html index.htm index.jsp;
}
}
上面两步配置完成之后,就可以在本机直接通过上面hosts里面绑定的域名进行访问。如图:
prometheus.dxm.com:
logstash.dxm.com:
tomcat.dxm.com:
grafana.dxm.com:
rocketmqconsole.dxm.com: