Nginx基础
Nginx软件包方式安装与配置
Nginx安装
两种安装方式:1、yum安装 2、安装包安装(此次演示本方式)
准备环境
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23安装编译环境
[root@localhost ~]# yum -y install gcc gcc-c++ automake autoconf libtool make wget vim
安装PCRE库
https://ftp.pcre.org/pub/pcre/pcre-8.44.tar.gz
官网:https://pcre.org/
[root@localhost ~]# wget https://ftp.pcre.org/pub/pcre/pcre-8.44.tar.gz
[root@localhost ~]# tar -zxvf pcre-8.44.tar.gz
[root@localhost ~]#cd pcre-8.44
[root@localhost pcre-8.44]#
[root@localhost pcre-8.44]# ./configure
[root@localhost pcre-8.44]#make
[root@localhost pcre-8.44]#make install
安装zlib库
http://zlib.net/zlib-1.2.11.tar.gz
官网:http://zlib.net/
[root@localhost ~]# wget http://zlib.net/zlib-1.2.11.tar.gz
[root@localhost ~]#tar -zxvf zlib-1.2.11.tar.gz
[root@localhost ~]# cd zlib-1.2.11
[root@localhost zlib-1.2.11]#make
[root@localhost zlib-1.2.11]#make install下载👉官网
选择稳定版
右键复制链接地址
下载解压编译
1
2
3
4
5
6[root@localhost ~]# wget http://nginx.org/download/nginx-1.20.0.tar.gz
[root@localhost ~]# tar -zxvf nginx-1.20.0.tar.gz
[root@localhost ~]# cd nginx-1.20.0
[root@localhost nginx-1.20.0]# ./configure
[root@localhost nginx-1.20.0]# make
[root@localhost nginx-1.20.0]# make install注意安装路径
基本命令
可设置环境变量或者使用软连接
ln -s /usr/local/nginx/sbin/nginx /sbin/nginx
启动、停止、重载就不需要加绝对路径或者进入nginx目录
例:1、nginx 启动 2、nginx -s stop 停止 3、nginx -s reload 重载
[root@localhost ~]# whereis nginx #可使用whereis nginx查找nginx所在的位置
nginx: /usr/sbin/nginx /usr/local/nginx
启动
1
2
3
4
5
6
7
8
9
10
11
12[root@localhost ~]# /usr/local/nginx/sbin/nginx #启动
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
[root@localhost ~]# ps -aux | grep nginx
root 25085 0.0 0.0 32976 380 ? Ss 00:44 0:00 nginx: master process ./nginx
nobody 25086 0.0 0.5 66580 4484 ? S 00:44 0:00 nginx: worker process
root 25147 0.0 0.1 12112 976 pts/0 S+ 00:51 0:00 grep --color=auto nginx
[root@localhost ~]#停止
1
2
3
4[root@localhost ~]# /usr/local/nginx/sbin/nginx -s stop #停止
[root@localhost ~]# ps -aux | grep nginx
root 25157 0.0 0.1 12112 1080 pts/0 R+ 00:51 0:00 grep --color=auto nginx
[root@localhost ~]#重启
1
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload #重新加载
Nginx配置文件详解
Nginx配置文件结构
全局块、events块、http块、server块、location块
1 | [root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf |
1 | 全局设置 |
Nginx反向代理
- 网络拓扑图
准备Tomcat环境
快速部署Tomcat
1
2
3
4
5
6
7
8
9
10
11
12
13[root@Tomcat ~]# yum install java-1.8.0-openjdk vim wget
[root@Tomcat ~]# java -version
openjdk version "1.8.0_292"
OpenJDK Runtime Environment (build 1.8.0_292-b10)
OpenJDK 64-Bit Server VM (build 25.292-b10, mixed mode)
[root@Tomcat ~]#
[root@Tomcat ~]# wget https://mirrors.bfsu.edu.cn/apache/tomcat/tomcat-10/v10.0.5/bin/apache-tomcat-10.0.5.tar.gz
[root@Tomcat ~]# tar -zxvf apache.tomcat-10.0.5.tar.gz
[root@Tomcat ~]# apache-tomcat-10.0.5/bin/startup.sh
关闭防火墙、selinux
[root@localhost sbin]# systemctl stop firewalld.service
[root@localhost sbin]# setenforce 0测试
配置Nginx反向代理
1
2
3
4[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
在location块添加如下配置
proxy_pass http://192.168.204.131:8080; #Tomcat服务器地址,注意端口号和;结尾
1 | 启动Nginx |
访问测试
我们Nginx的IP地址为192.168.204.135,Tomcat的IP地址为192.168.204.131,从上面Tomcat测试即可看出。
通过访问Nginx服务器地址能够打开Tomcat页面则反向代理成功
负载均衡
- 网络拓扑图
搭建Tomcat服务器
需要搭建两台Tomcat服务器
- Tomcat01
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16[root@Tomcat01 ~]# yum install java-1.8.0-openjdk wget vim
[root@Tomcat01 ~]# java -version
openjdk version "1.8.0_292"
OpenJDK Runtime Environment (build 1.8.0_292-b10)
OpenJDK 64-Bit Server VM (build 25.292-b10, mixed mode)
[root@Tomcat01 ~]#
[root@Tomcat01 ~]# wget https://mirrors.bfsu.edu.cn/apache/tomcat/tomcat-10/v10.0.5/bin/apache-tomcat-10.0.5.tar.gz
[root@Tomcat01 ~]# tar -zxvf apache.tomcat-10.0.5.tar.gz
[root@Tomcat01 ~]# apache-tomcat-10.0.5/bin/startup.sh
关闭防火墙、selinux
[root@Tomcat01 ~]# systemctl stop firewalld.service
[root@Tomcat01 ~]# setenforce 0
修改主页方便区分
[root@Tomcat01 ~]# vim apache-tomcat-10.0.5/webapps/ROOT/index.jsp
- Tomcat02
1 | [root@Tomcat02 ~]# yum install java-1.8.0-openjdk wget vim |
- 测试Tomcat01和02服务器
配置Nginx负载均衡
安装请参考
一、Nginx安装
配置Nginx文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19[root@Nginx ~]# vim /usr/local/nginx/conf/nginx.conf
upstream 192.168.204.135 { #可更改为域名和proxy_pass对应
server 192.168.204.131:8080; # Tomcat01服务器IP地址:端口;
server 192.168.204.132:8080; # Tomcat02服务器IP地址:端口
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_pass http://192.168.204.135; #本机IP地址:可更改为域名
}重启
1
[root@Nginx ~]# /usr/local/nginx/sbin/nginx #启动或重载 -s reload
测试,访问Nginx服务器
刷新
权重轮询
假如我们两台服务器Tomcat01的性能优于Toncat02我们可以通过配置权重,让大部分请求交给Tomcat01处理
1 | upstream 192.168.204.135 { |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 尤妤!