Nginx服务
安装和启动
- 解压源码
tar -xvf nginx-xx.xx.xx.tar.gz
- 配置(模块有很多,这里以ssl为例)
# 配置安装路径,指定服务运行时使用的用户,安装HTTP SSL模块
./configure --prefix=/usr/local/nginx --user=nginx --with-http_ssl_module
- 编译和安装
make -j4 && make install
- 创建用户
user -M nginx -s /sbin/nologin
- 启动,关闭,重启,查看配置
# 启动
/usr/local/nginx/sbin/nginx
# 关闭 killall nginx也可行
/usr/local/nginx/sbin/nginx -s stop
# 重新加载配置
/usr/local/nginx/sbin/nginx -s reload
# 查看版本和配置
/usr/local/nginx/sbin/nginx -V
网站认证
- 编辑配置文件
vim /usr/local/nginx/conf/nginx.confserver {...auth_basic "请输入用户名和密码"; # 提示信息auth_basic_user_file "/usr/local/nginx/pass"; # 存放网站账户的文件...
}
- 创建验证文件
yum install -y httpd-tools
# 第一次需要创建pass文件,所以需要加-c
htpasswd -c /usr/local/nginx/pass test
New password: # 输入密码
Re-type new password: # 再次输入密码
Adding password for user test01# 添加第二个用户,不要加-c
htpasswd /usr/local/nginx/pass bhlu
New password: # 输入密码
Re-type new password: # 再次输入密码
Adding password for user test02
- 重新加载配置
/usr/local/nginx/sbin/nginx -s reload
多域名访问
- 编辑配置文件
vim /usr/local/nginx/conf/nginx.confhttp {...server {listen 80; server_name www.a.com; # www.a.com这个域名root html_b; # 网站根目录index index.html; # 主页文件}server {listen 80; server_name www.b.com; # www.b.com这个域名root html_b; # 网站根目录index index.html; # 主页文件}
}
- 创建网站根目录和主页文件
mkdir /usr/local/nginx/{html_a,html_b}
echo "aaa" > /usr/local/nginx/html_a/index.html
echo "bbb" > /usr/local/nginx/html_b/index.html
- 重新加载配置
/usr/local/nginx/sbin/nginx -s reload
配置加密网站
- 编辑配置文件
vim /usr/local/nginx/conf/nginx.confserver {listen 443 ssl;server_name localhost;ssl_certificate cert.pem; # 公钥ssl_certificate_key cert.key; # 私钥ssl_session_cache shared:SSL:1m;ssl_session_timeout 5m;ssl_ciphers HIGH:!aNULL:!MD5;ssl_prefer_server_ciphers on;location / {root https; # 设置网站根目录index index.html index.htm;}
}
- 创建网站根目录,并创建主页文件
mkdir /usr/local/nginx/https
echo "test https~~~" > /usr/local/nginx/https/index.html
- 生成私钥
openssl genrsa > /usr/local/nginx/conf/cert.key
# 输出
Generating RSA private key, 2048 bit long modulus
..........................................................................................................................................................................................+++
........................+++
e is 65537 (0x10001)
- 根据私钥生成公钥
openssl req -x509 -key /usr/local/nginx/conf/cert.key > /usr/local/nginx/conf/cert.pem
# 输出
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:cn # 国家
State or Province Name (full name) []:JiangSu # 省份
Locality Name (eg, city) [Default City]:WuXi # 城市
Organization Name (eg, company) [Default Company Ltd]:test # 公司
Organizational Unit Name (eg, section) []:test # 部门
Common Name (eg, your name or your server's hostname) []:test-server # 服务器名称
Email Address []:test@test.com # 电子邮件
- 重新加载配置
/usr/local/nginx/sbin/nginx -s reload
- 进行测试
curl -k https://www.a.com
# test https~~~
LNMP
LNMP环境
L
:linux
操作系统N
:Nginx
网站服务M
:mariadb(mysql)
数据库P
:php
编写动态网站的语言工具
- 初始化
nginx
服务
# 关闭之前实验的nginx,并将配置恢复默认
killall nginx
cp /usr/local/nginx/conf/nginx.conf.default /usr/local/nginx/conf/nginx.conf
cp:是否覆盖"/usr/local/nginx/conf/nginx.conf"? y
- 安装相关软件包
# 数据库相关,这里为了简单,使用的是mariadb,mariadb(数据库客户端)、mariadb-server(数据库服务端)、mariadb-devel(数据库开发环境依赖包)
yum install -y mariadb mariadb-server mariadb-devel
systemctl start mariadb && systemctl enable mariadb
systemctl status mariadb# php相关,php(解释器)、php-fpm(帮助nginx解析php编写的动态网站的服务)、php-mysql(php与mysql关联的软件包)
yum install -y php php-fpm php-mysql
systemctl start php-fpm && systemctl enable php-fpm
systemctl status php-fpm
# php-fpm的配置文件在/etc/php-fpm.d/www.conf,里面包含了端口,服务端口,最小守护进程数量等,一个服务进程大概是25M左右
- 准备动态网站页面
cp /data/test.php /usr/local/nginx/html/
- 启动
nginx
服务,设置相关配置
# 启动
/usr/local/nginx/sbin/nginx# 编辑配置文件
vim /usr/local/nginx/conf/nginx.conf
...
# 匹配以.php结尾的
location ~ \.php$ {root html; # 网站位置fastcgi_pass 127.0.0.1:9000; # 找本地9000端口fastcgi_index index.php; # 默认主页# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # 这行无用,需要注释include fastcgi.conf; # 这里需要修改,使用了是另一个配置文件
}
...# 重新加载配置
/usr/local/nginx/sbin/nginx -s reload
地址重写
格式:rewrite 匹配路径 实际路径 选项
- 选项
redirect
:临时重定向,302permanent
:永久重定向,301last
:不再读其他rewrite
,还是除rewrite
之外的其他语句的break
:不再读其他语句
- 访问
a.html
实际显示b.html
vim /usr/local/nginx/conf/nginx.conf...
rewrite ^/a\.html$ /b.html;
...# 重新加载配置
/usr/local/nginx/sbin/nginx -s reload
- 访问本地的任意页面,跳转到指定网站的同页面
vim /usr/local/nginx/conf/nginx.conf...
rewrite /(.*) http://www.test.com/$1;
...# 重新加载配置
/usr/local/nginx/sbin/nginx -s reload
- 根据浏览器的不同访问不同的页面
vim /usr/local/nginx/conf/nginx.conf...
# $http_user_agent是nginx内置变量,存储了用户的信息
if ($http_user_agent ~* firefox){rewrite /(.*) /firefox/$1;
}
...# 重新加载配置
/usr/local/nginx/sbin/nginx -s reload
last
选项
# last
vim /usr/local/nginx/conf/nginx.conf...
server {...location / {rewrite /a.html /b.html last; # 这里使用last,不再读其他的rewrite...}rewrite /b.html /c.html;
}
...# 重新加载配置
/usr/local/nginx/sbin/nginx -s reload# 测试
curl 127.0.0.1/a.html # 结果是b.html的页面
break
选项
# last
vim /usr/local/nginx/conf/nginx.conf...
server {...location / {rewrite /a.html /b.html break; # 这里如果使用的是last,那么访问a.html,会显示c.html的内容,所以这里需要使用break...}location /b.html {rewrite /b.html /c.html;}
}
...# 重新加载配置
/usr/local/nginx/sbin/nginx -s reload# 测试
curl 127.0.0.1/a.html # 结果是b.html的页面
代理功能(网站业务)
环境
proxy
:192.168.3.1web1
:192.168.3.100web2
:192.168.3.200
- 开启
web1
和web2
的网站服务(这里使用httpd
服务)
# web1和web2
yum install -y httpd
systemctl start httpd# web1
echo "web1" > /var/www/html/index.html# web2
echo "web2" > /var/www/html/index.html
- 在
proxy
主机上使用nginx
的代理功能
# 将之前的nginx服务删除
killall nginx
rm -rf /usr/local/nginx# 安装nginx
tar -xvf nginx-1.17.6.tar.gz
cd nginx-1.17.6/
./configure
make && make install# 编辑配置文件
vim /usr/local/nginx/conf/nginx.conf
...
http {...# 创建集群,集群名称叫做webupstream web {server 192.168.3.100:80;server 192.168.3.200:80;}server {listen 80;...localtion / {proxy_pass http://web; # 调用集群root html;index index.html index.htm;}...}...
}
...# 启动nginx
/usr/local/nginx/sbin/nginx
- 测试是否成功
for i in {1..5}; do curl 192.168.3.1; done
# web1 web2 web1 web2 web1
- 集群优化,设置权重,配置健康检查,相同客户机访问相同服务器
# 编辑配置文件
vim /usr/local/nginx/conf/nginx.conf
...
http {...# 创建集群,集群名称叫做webupstream web {server 192.168.3.100:80;# weight=2: 权重2,默认权重是1# max_fails=2: 检测两次如果都失败,则判为故障# fail_timeout=30: 故障机器30s再次测试server 192.168.3.200:80 weight=2 max_fails=2 fail_timeout=30;;}server {listen 80;...localtion / {proxy_pass http://web; # 调用集群root html;index index.html index.htm;}...}...
}
...# 重新加载配置
/usr/local/nginx/sbin/nginx -s reload
- 再次测试
for i in {1..5}; do curl 192.168.3.1; done
# web1 web2 web2 web1 web2
如果想要将集群中某个主机不参与集群活动
server 192.168.3.200 down;即可
四层代理(其他业务)
环境
proxy
:192.168.3.1server1
:192.168.3.100server2
:192.168.3.200
- 将nginx恢复默认配置
/usr/local/nginx/sbin/nginx -s stop
cp /usr/local/nginx/conf/nginx.conf.default /usr/local/nginx/conf/nginx.conf
cp:是否覆盖"/usr/local/nginx/conf/nginx.conf"? y
- 重新配置编译,添加四层代理模块
# 先查看之前nginx编译所带模块
/usr/local/nginx/sbin/nginx -V
# 因为上个实验没有加模块,所以最后一行只有configure arguments: cd ~/nginx-1.17.6/
./configure --with-stream # --with-stream 四层代理,如果之前的nginx有模块,需要在后面加上去
make # 注意这里只需编译即可,不需要安装# 检查新编译的nginx是否符合要求
./objs/nginx -V
# 最后一行: configure arguments: --with-stream# 将新编译的nginx拷贝过去
cp ./objs/nginx /usr/local/nginx/sbin/nginx
cp:是否覆盖"/usr/local/nginx/sbin/nginx"? y# 启动nginx试试
/usr/local/nginx/sbin/nginx
- 配置四层代理
vim /usr/local/nginx/conf/nginx.conf...
# 新业务,跟http同级
stream {# 新集群,叫backend,这里以集群的sshd服务作为示例upstream backend {server 192.168.3.100:22;server 192.168.3.200:22;}server {listen 10022; # 监听端口号,访问这个端口号,就会跳转到集群中proxy_pass backend; # 调用集群}
}http {...
}# 加载配置,如果安装的nginx没有加--with-stream,这里会报错
/usr/local/nginx/sbin/nginx -s reload
- 测试是否成功
ssh -p 10022 root@192.168.3.1
# 进入server1ssh -p 10022 root@192.168.3.1
# 进入server2
常见的nginx问题
- 配置404报错
vim /usr/local/nginx/conf/nginx.conf...
http {...server {...error_page 404 /404.html; # 设置404页面...}
}
...
- 查看网站后台数据
# 需要添加一个新的模块--with-http_stub_status_module
# 查看现在的nginx
killall nginx
/usr/local/nginx/sbin/nginx -V
# nginx version: nginx/1.17.6
# built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
# configure arguments: --with-stream# 重新编译并拷贝过去
cd ~/nginx-1.17.6/
./configure --with-stream --with-http_stub_status_module # 添加上之前的模块
make
cp ./objs/nginx /usr/local/nginx/sbin/nginx
cp:是否覆盖"/usr/local/nginx/sbin/nginx"? y# 编辑配置文件
vim /usr/local/nginx/conf/nginx.conf
...
http {...server {...location / {root html;index index.html index.htm;}# 添加下面几行location /status {stub_status on; # 显示后台的数据allow 192.168.3.1; # 只允许192.168.3.1查看deny all; # 拒绝其他}...}
}
...# 启动nginx
/usr/local/nginx/sbin/nginx# 本机测试查看,其他机器访问会报错403
curl 192.168.3.1/status
: << 'EOF'
Active connections: 1
server accepts handled requests14 14 14
Reading: 0 Writing: 1 Waiting: 0Active connections: 当前活动的连接数量,即当前有多少用户访问该网站
accepts: 已接收客户端的连接总数
handled: 已处理客户端的连接总数
requests: 客户端发送的请求数
Reading: 当前服务器正在读取客户端请求头的数量
Writing: 当前服务器正在写响应信息的数量
Waiting: 当前多少客户端在等待服务器的响应EOF