目录
1、Nginx基础
1.1Nginx和Apache的差异
1.2Nginx和Apache的优缺点比较
1.3编译安装Nginx服务
2、认识Nginx服务的主配置文件 nginx.conf
1、全局配置
2、I/O事件配置
3、HTTP设置
4、访问状态统计配置
5、基于授权的访问控制
6、基于客户端的访问控制
7、基于域名的Nginx虚拟主机
8、基于IP的Nginx虚拟主机
9、基于端口的Nginx虚拟主机
nginx相对于apache的优点∶
apache相对于nginx的优点∶
存在就是理由,一般来说,需要性能的web服务,用Nginx。如果不需要性能只求稳定,那就Apache。Nginx处理动态请求是弱项,一般动态请求要Apache去做,Nginx只适处理静态网页或反向代理。
1、关闭防火墙
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# setenforce 0
nginx的配置及运行需要pcre、zlib等软件包的支持,因此需要安装这些软件的开发包,以便提供相应的库和头文件。
[root@localhost ~]# yum -y install pcre-devel zlib-devel gcc gcc-c++ make
3、创建运行用户、组
Nginx 服务程序默认以 nobody 身份运行,建议为其创建专门的用户账号,以便更准确地控制其访问权限
[root@localhost ~]# useradd -M -s /sbin/nologin/ nginx
4、将安装nginx所需的软件包传到/opt目录下并解压
[root@localhost ~]# cd /opt
[root@localhost opt]# ls
rh
[root@localhost opt]# rz -E
rz waiting to receive.
[root@localhost opt]# ls
nginx-1.12.0.tar.gz rh
[root@localhost opt]# tar zxvf nginx-1.12.0.tar.gz -C /opt/
nginx-1.12.0/
5、编译安装Nginx
配置
[root@localhost opt]# ls
nginx-1.12.0 nginx-1.12.0.tar.gz rh
[root@localhost opt]# cd nginx-1.12.0/
[root@localhost nginx-1.12.0]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@localhost nginx-1.12.0]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module
编译安装
[root@localhost nginx-1.12.0]# make && make install
6、优化路径
[root@localhost local]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
创建软连接 方便系统识别
7、 检查、启动、重启、停止Nginx服务
nginx -t #检查配置文件是否配置正确
nginx #启动
#停止
cat /usr/local/nginx/logs/nginx.pid #先查看nginx的PID号
kill -3
或者
killall -3 nginx
killall -s QUIT nginx
------------------------------------------------
#重载
kill -1
kill -s HUP
killall -1 nginx
killall -s HUP nginx
-----------------------------------------------
#日志分隔,重新打开日志文件
kill -USR1
-----------------------------------------------
#平滑升级
kill -USR2
------------------------------------------------
新版本升级
tar -zxvf nginx-1.xx.xx.tar.gz
cd nginx-1.xx.xx
./configure \
-prefix=/usr/local/ngink \
-user=nginx\
-group=nginx \
-with-http stub_status _module\
-with-http_ssl_module
make
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_old
cp objs/nginx /usr/local/nginx/sbin/nginx
make upgrade
#或者先killall nginx,再/usr/local/nginx/sbin/nginx
开启
关闭
法一
[root@localhost sbin]# nginx
[root@localhost sbin]# netstat -natp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6783/nginx: master
[root@localhost sbin]# cat /usr/local/nginx/logs/nginx.pid
6783
[root@localhost sbin]# kill -3 6783
[root@localhost sbin]# netstat -natp | grep nginx
法二
8、添加 Nginx 系统服务
法一
vim /etc/init.d/nginx
#!/bin/bash
#chkconfig: 35 99 20
#description:Nginx Service Control Script
COM="/usr/local/nginx/sbin/nginx"
PID="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$COM
;;
stop)
kill -s QUIT $(cat $PID)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PID)
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0
chmod +x /etc/init.d/nginx
chkconfig --add nginx #添加为系统服务
systemctl stop nginx
systemctl start nginx
法二
vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
chmod 754 /lib/systemd/system/nginx.service
systemctl start nginx.service
systemctl enable nginx.service
-----------------------------------------------------
[Unit]:服务的说明
Description :描述服务
After:依赖,当依赖的服务启动之后再启动自定义的服务
[service]服务运行参数的i设置
Type=forking是后台运行的形式,使用此启动类型应同时指定
PIDFile=,以便systemd能够跟踪服务的主进程。Execstart为服务的具体运行命令
ExecReload为重启命令
Execstop为停止命令
PrivateTrmp=True丧示给服务分配独立.的临时空间
注意:启动、重启、停止命令全部要求使用绝对路径
[Insta11]服务安装的相关设置,可设置为多用户
配置文件包含的内容
vim /usr/local/nginx/conf/nginx.conf
#user nobody; #运行用户,若编译时未指定则默认为 nobody
worker_ processes 1; #工作运行数量,一般设置为和CPU核数一样
#error_log logs/error.log; #错误日志文件的位置
#pid logs/nginx.pid; #PID文件的位置
events {
use epoll; #使用 epoll 模型以提高性能,2.6 以上版本建议使用
worker_connections 1024; #每个进程处理1024个连接
}
允许 Nginx 正常提供服务的连接数(并发量):工作进程数为 1,每个进程处理 1024 个连接,则为cpu核数*连接数(1*1024=1024)
内核优化:
vim /etc/security/limits.conf
http {
##文件扩展名与文件类型映射表
include mime.types;
##默认文件类型
default_type application/octet-stream;
##日志格式设定
#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 logs/access.log main;
##开启文件传输模式
sendfile on;
##减少网络报文段的数量
#tcp_nopush on;
##连接保持超时时间,单位是秒
#keepalive_timeout 0; #不开启长连接
keepalive_timeout 65;
##gzip模块设置,设置是否开启gzip压缩输出
#gzip on;
##Web 服务的监听配置
server {
##监听地址及端口
listen 80;
##站点域名,可以有多个,用空格隔开
server_name localhost;
##网页的默认字符集
charset utf-8;
##根目录配置
location / {
##网站根目录的位置/usr/local/nginx/html
root html;
##默认首页文件名
index index.html index.htm;
}
##内部错误的反馈页面
error_page 500 502 503 504 /50x.html;
##错误页面配置
location = /50x.html {
root html;
}
}
}
日志格式设定:
$remote_addr与$http_x_forwarded_for用以记录客户端的ip地址;
$remote_user:用来记录客户端用户名称;
$time_local: 用来记录访问时间与时区;
$request: 用来记录请求的url与http协议;
$status: 用来记录请求状态;成功是200,
$body_bytes_sent :记录发送给客户端文件主体内容大小;
$http_referer:用来记录从那个页面链接访问过来的;
$http_user_agent:记录客户浏览器的相关信息;
通常web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了,
通过$remote_add拿到的IP地址是反向代理服务器的iP地址。
反向代理服务器在转发请求的http头信息中,
可以增加x_forwarded_for信息,用以记录原有客户端的IP地址和原来客户端的请求的服务器地址。
location常见配置指令, root(根路径配置)、alias(别名配置)、proxy_ pass (反向代理配置)
root(根路径配置)∶ 请求root /var/www/html
请求www.kgc.com/test/1.html,会返回文件/var/www/html/test/1.html
alias(别名配置):alias /var/www/html
请求www.kgc.com/test/1.html,会返回文件/var/www/html/1.html
root (根)
第一种情况
如果访问http://192.168.52.110/my/test/test.html 访问的路径为:/usr/local/nginx/html/my/test/test.html
第二种情况
如果访问http://192.168.52.110/my/test/test.html 访问的路径为:/usr/local/nginx/html/my/test/test.html
alias(别名)
[root@localhost ~]# mkdir -p /var/www/html
[root@localhost ~]# cd /var/www/html
[root@localhost html]# ls
[root@localhost html]# echo "this is var" > test.html
[root@localhost html]# ls
test.html
[root@localhost html]# cat test.html
this is var
[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf
------------------------------------------------------------------
location /test {
alias /var/www/html;
index index.html index.htm;
}
[root@localhost html]# nginx -t #检查语法
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost html]# systemctl restart nginx #重启服务
1、查看安装模块
/usr/local/sbin/nginx -V #查看已安装Nginx是否包含HTTP_STUB_STATUS模块
cat /opt/nginx-1.12.0/auto/options | grep YES #查看nginx已安装的所有模块
2、修改 nginx.conf 配置文件,指定访问位置并添加 stub_status 配置
[root@localhost ~]#cd /usr/local/nginx/conf
[root@localhost conf]# ls
fastcgi.conf fastcgi_params.default mime.types nginx.conf.default uwsgi_params
fastcgi.conf.default koi-utf mime.types.default scgi_params uwsgi_params.default
fastcgi_params koi-win nginx.conf scgi_params.default win-utf
[root@localhost conf]# cp nginx.conf nginx.conf.bak #备份配置文件 以防配置出错
vim /usr/local/nginx/conf/nginx.conf
......
http {
......
server {
listen 80;
server_name localhost;
charset utf-8;
location / {
root html;
index index.html index.htm;
}
-----------------------------------------------------------
##添加 stub_status 配置##
location /status { #访问位置为/status
stub_status on; #打开状态统计功能
access_log off; #关闭此位置的日志记录
}
}
}
[root@localhost conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost conf]# systemctl restart nginx
3、重启服务,访问测试
浏览器访问 http://192.168.52.110/status
Active connections :表示当前的活动连接数;
server accepts handled requests :表示已经处理的连接信息,三个数字依次表示已处理的连接数、成功的TCP握手次数、 已处理的请求数。
可以curl -Ls http://192.168.52.110/status结合awk来写shell脚本,如果链接数过高报警
vim 1.sh
-------------------------------------------------------
#!/bin/bash
con=`curl -Ls http://192.168.239.10/status | awk 'NR==1{print $3}'`
if [ $con -gt 20000 ]
then
echo "当前并发数量为 $con ,超过预警值"
fi
1、生成用户密码认证文件
[root@localhost nginx]# pwd
/usr/local/nginx
[root@localhost nginx]# yum install httpd-tools -y
[root@localhost nginx]# htpasswd -c ./usrlist.txt my
New password:
Re-type new password:
Adding password for user my
[root@localhost nginx]# ls
client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp usrlist.txt uwsgi_temp
[root@localhost nginx]# cat usrlist.txt
my:$apr1$S9xb3mEi$y/2OiJE58zFme0huBGJ6S/
[root@localhost nginx]# htpasswd ./usrlist.txt qyd
New password:
Re-type new password:
Adding password for user qyd
[root@localhost nginx]# cat usrlist.txt
my:$apr1$S9xb3mEi$y/2OiJE58zFme0huBGJ6S/
qyd:$apr1$wBueVUlF$NOd6jD46F.w.ulqxrE06M/
[root@localhost nginx]# ll
总用量 8
drwx------. 2 nginx root 6 5月 10 16:29 client_body_temp
drwxr-xr-x. 2 root root 4096 5月 10 21:53 conf
drwx------. 2 nginx root 6 5月 10 16:29 fastcgi_temp
drwxr-xr-x. 3 root root 52 5月 10 20:47 html
drwxr-xr-x. 2 root root 58 5月 10 21:54 logs
drwx------. 2 nginx root 6 5月 10 16:29 proxy_temp
drwxr-xr-x. 2 root root 19 5月 10 16:20 sbin
drwx------. 2 nginx root 6 5月 10 16:29 scgi_temp
-rw-r--r--. 1 root root 83 5月 10 22:11 usrlist.txt
drwx------. 2 nginx root 6 5月 10 16:29 uwsgi_temp
[root@localhost nginx]# chown nginx usrlist.txt
[root@localhost nginx]# chmod 400 usrlist.txt
[root@localhost nginx]# ll
总用量 8
drwx------. 2 nginx root 6 5月 10 16:29 client_body_temp
drwxr-xr-x. 2 root root 4096 5月 10 21:53 conf
drwx------. 2 nginx root 6 5月 10 16:29 fastcgi_temp
drwxr-xr-x. 3 root root 52 5月 10 20:47 html
drwxr-xr-x. 2 root root 58 5月 10 21:54 logs
drwx------. 2 nginx root 6 5月 10 16:29 proxy_temp
drwxr-xr-x. 2 root root 19 5月 10 16:20 sbin
drwx------. 2 nginx root 6 5月 10 16:29 scgi_temp
-r--------. 1 nginx root 83 5月 10 22:11 usrlist.txt
drwx------. 2 nginx root 6 5月 10 16:29 uwsgi_temp
2、修改主配置文件相对应目录,添加认证配置项
[root@localhost nginx]# vim /usr/local/nginx/conf/nginx.conf
-------------------------------------------------------------------
location /test {
alias /var/www/html;
index index.html index.htm;
auth_basic "Hello everyone";
auth_basic_user_file /usr/local/nginx/usrlist.txt;
}
3、重启服务,访问测试
[root@localhost nginx]# nginx -t #检查语法
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost nginx]# systemctl restart nginx #重启服务
输入网址http://192.168.52.110/test/test.html
Nginx 基于客户端的访问控制规则如下:
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
---------------------------------------------------
location /test {
alias /var/www/html;
index index.html index.htm;
auth_basic "Hello everyone";
auth_basic_user_file /usr/local/nginx/usrlist.txt;
deny 192.168.52.110;
}
重启服务 ,访问测试
[root@localhost ~]# systemctl restart nginx
虚拟主机有三种:基于域名、基于IP、基于端口
1、为虚拟主机提供域名解析
[root@localhost ~]# vim /etc/hosts
----------------------------------------------------------------------------------
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.52.110 www.my.com www.qyd.com
2、为虚拟主机准备网页文档
[root@localhost conf]# cd /var/www/html/
[root@localhost html]# ls
test.html
[root@localhost html]# mkdir my qyd
[root@localhost html]# ls
my qyd test.html
[root@localhost html]# echo "this is my test web" > my/index.html
[root@localhost html]# echo "this is qyd test web" > qyd/index.html
3、修改Nginx配置文件
[root@localhost ~]# cd /usr/local/nginx/conf/
[root@localhost conf]# ls
fastcgi.conf fastcgi_params.default mime.types nginx.conf.bak scgi_params.default win-utf
fastcgi.conf.default koi-utf mime.types.default nginx.conf.default uwsgi_params
fastcgi_params koi-win nginx.conf scgi_params uwsgi_params.default
[root@localhost conf]# cp nginx.conf.default nginx.conf #还原默认的配置文件
cp:是否覆盖"nginx.conf"? y
----------------------------------------------------------------
server {
listen 80;
server_name www.my.com;
charset utf-8;
access_log logs/my.com.access.log;
location / {
root /var/www/html/my;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
server {
listen 80;
server_name www.qyd.com;
charset utf-8;
access_log logs/qyd.com.access.log;
location / {
root /var/www/html/qyd;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
重启服务 ,输入域名进行测试
[root@localhost conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost conf]# systemctl restart nginx
1、设置虚拟IP
[root@localhost ~]# ifconfig ens33:0 192.168.52.11/24
[root@localhost ~]# ifconfig
ens33: flags=4163 mtu 1500
inet 192.168.52.110 netmask 255.255.255.0 broadcast 192.168.52.255
inet6 fe80::d6fa:c8c:b90d:12db prefixlen 64 scopeid 0x20
ether 00:0c:29:e6:f0:79 txqueuelen 1000 (Ethernet)
RX packets 85179 bytes 76908377 (73.3 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 38826 bytes 9886685 (9.4 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:0: flags=4163 mtu 1500
inet 192.168.52.11 netmask 255.255.255.0 broadcast 192.168.52.255
ether 00:0c:29:e6:f0:79 txqueuelen 1000 (Ethernet)
2、编辑配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
--------------------------------------------------------------
server {
listen 192.168.52.110:80;
server_name www.my.com;
charset utf-8;
access_log logs/my.com.access.log;
location / {
root /var/www/html/my;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
server {
listen 192.168.52.11:80;
server_name www.qyd.com;
charset utf-8;
access_log logs/qyd.com.access.log;
location / {
root /var/www/html/qyd;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
3、重启并测试
[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# systemctl restart nginx
1、修改配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
---------------------------------------------------------------
server {
listen 192.168.52.110:330;
server_name www.my.com;
charset utf-8;
access_log logs/my.com.access.log;
location / {
root /var/www/html/my;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
server {
listen 192.168.52.110:30;
server_name www.qyd.com;
charset utf-8;
access_log logs/qyd.com.access.log;
location / {
root /var/www/html/qyd;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
2、重启并测试
[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# systemctl restart nginx