目录
1、关闭防火墙,将安装ngnix所需的软件包上传到/opt目录下
前言
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好。
一、编译安装Nginx服务
1、关闭防火墙,将安装ngnix所需的软件包上传到/opt目录下
systemctl stop firewalld
systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
setenforce 0
2、安装依赖包
yum -y install pcre-devel zlib-devel gcc gcc-c++ make
3、编译安装Nginx
tar zxvf nginx-1.12.2.tar.gz -C /opt/
cd nginx-1.12.2/
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
make && make install
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
cd /opt
useradd -M -s /sbin/nologin nginx
4、检查、启动、重启、停止nginx服务
nginx -t #检查配置文件是否配置正确
nginx #启动
cat /usr/local/nginx/logs/nginx.pid #先查看nginx的PID号
kill -3 <PID号>
kill -s QUIT <PID号> #停止
killall -3 nginx
killall -s QUIT nginx
kill -1 <PID号> #重载
kill -s HUP <PID号>
killall -1 nginx
killall -s HUP nginx #日志分隔,重新打开日志文件
kill -USR1 <PID号> #平滑升级
kill -USR2 <PID号>
5、添加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
ExecrReload=/bin/kill -s HUP $MAINPID
ExecrStop=/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
注意:这里可能会出现不能正常启动的问题,之前你有装过apache,是有冲突的,得停止nginx的进程,再重新启动才行
killall -3 nginx
nginx
二、Nginx配置文件
vim /usr/local/nginx/conf/nginx.conf
1、全局配置
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
2、I/O事件配置
events {
use epoll; #epoll 模型,多路复用机制,进程协程+回调实现了高并发能力,以提高性能
worker_connections 1024; #每个进程处理 4096 个连接,受最大文件打开数和cpu的制约
#如提高每个进程的连接数还需执行“ulimit -n 65535”命令临时修改本地每个进程可以同时打开的最大文件数。
#在Linux平台上,在进行高并发TCP连接处理时,最高的并发数量都要受到系统对用户单一进程同时可打开文件数量的限制(这是因为系统为每个TCP连接都要创建一个socket句柄,每个socket句柄同时也是一个文件句柄)。
#可使用ulimit -a命令查看系统允许当前用户进程打开的文件数限制。
3、HTTP配置
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;
##此选项允许或禁止使用socke的TCP_CORK的选项(发送数据包前先缓存数据),此选项仅在使用sendfile的时候使用
#tcp_nopush on;
##连接保持超时时间,单位是秒
#keepalive_timeout 0;
keepalive_timeout 65;
##gzip模块设置,设置是否开启gzip压缩输出
#gzip on;
##Web 服务的监听配置
server {
##监听地址及端口
listen 80;
##站点域名,可以有多个,用空格隔开
server_name www.lic.com;
##网页的默认字符集
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;
}
}
}
三、状态访问统计
1、查看是否包含HTTP_STUB_STATUS模块
cd /usr/local/nginx/conf/
nginx -V
2、修改配置文件,并添加stub_status配置
cp nginx.conf nginx.conf.bak
vim /usr/local/nginx/conf/nginx.conf
http {
......
server {
listen 80;
server_name www.nginx.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
##添加stub_status配置
location /status { #访问位置为/status : www.nginx.com/status
stub_status on; #打开状态统计功能
access_log off; #关闭此位置的日志功能
}
3、重启服务
vim /etc/hosts
nginx -t
systemctl restart nginx.service
四、访问控制
1、基于授权的访问控制
1-1、生成用户密码认证文件
yum install -y httpd-tools
htpasswd -c /usr/local/nginx/passwd.db zhangsan
chown nginx /usr/local/nginx/passwd.db
chmod 400 /usr/local/nginx/passwd.db
ls -l /usr/local/nginx/passwd.db
1-2、修改主配置文件相对应目录,添加认证配置项
vim /usr/local/nginx/conf/nginx.conf
......
server {
location / {
......
##添加认证配置##
auth_basic "secret";
auth_basic_user_file /usr/local/nginx/passwd.db;
}
}
1-3、重启服务,访问测试
nginx -t
systemctl restart nginx
2、基于客户端的访问控制
2-1、访问控制规则如下:
deny IP/IP段:拒绝某个IP或IP段的客户端访问
allow IP/IP段:允许某个IP或IP段的客户端的访问规则从上往下执行,如匹配则停止,不再往下匹配
vim /usr/local/nginx/conf/nginx.conf
......
server {
location / {
......
##添加控制规则##
deny 192.168.226.160; #拒绝访问的客户端 IP
allow all; #允许其它IP客户端访问
}
}
systemctl restart nginx
五、虚拟主机的应用
1、基于域名的 Nginx虚拟主机
1-1、添加域名解析
1-2、准备虚拟站点网页文档
mkdir -p /var/www/html/accp
mkdir -p /var/www/html/benet
echo "<h1>www.accp.com</h1>" > /var/www/html/accp/index.html
echo "<h1>www.benet.com</h1>" > /var/www/html/benet/index.html
1-3、修改配置文件
vim /usr/local/nginx/conf/nginx.conf
http {
......
#gzip on;
server {
listen 80;
server_name www.accp.com; ##设置域名www.accp.com
charset utf-8;
access_log logs/accp.access.log;
location / {
root /var/www/html/accp; ##设置www.accp.com 的工作目录
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name www.benet.com; ##设置域名www.benet.com
charset utf-8;
access_log logs/benet.access.log;
location / {
root /var/www/html/benet;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
2、基于端口的虚拟主机
2-1、创建8080端口的网页文件
cd /var/www/html/
ls
mkdir accp8080
cd accp8080
vim index.html
<h1> this is accp8080 </h1>
cat index.html
2-2、修改配置文件
vim /usr/local/nginx/conf/nginx.conf
http {
......
#gzip on;
server {
listen 192.168.159.70:80;
server_name www.accp.com;
charset utf-8;
access_log logs/accp.access.log;
location / {
root /var/www/html/accp;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 192.168.159.70:8080;
server_name www.accp.com;
charset utf-8;
access_log logs/accp8080.access.log;
location / {
root /var/www/html/accp8080;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
2-3、重启、验证、查看日志
nginx -t
systemctl restart nginx
查看日志:
3、基于不同IP访问
3-1、添加192.168.159.100的映射
vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.159.70 www.accp.com
192.168.159.100 www.benet.com
3-2、创建网站根目录、创建192.168.159.100的网站首页
cd /var/www/html/
ls
mkdir benet100
ls
cd benet100/
vim index.html
<h1> this is benet100 web </h1>
3-3、修改配置文件,
cd /usr/local/nginx/conf/
vim nginx.conf
http {
......
server {
listen 192.168.159.70:80;
server_name www.accp.com;
charset utf-8;
access_log logs/accp.access.log;
location / {
root /var/www/html/accp;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 192.168.159.100:80;
server_name www.benet.com;
charset utf-8;
access_log logs/benet100.access.log;
location / {
root /var/www/html/benet100;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
3-4、临时创建虚拟网卡
ifconfig ens33:0 192.168.159.100 netmask 255.255.255.0
ifconfig
3-5、重启、验证
nginx -t
systemctl restart nginx
总结
nginx的优势是处理静态请求,使用更少的资源,支持更多的并发连接,体现更高的效率,cpu内存使用率低,apache适合处理动态请求,所以现在一般前端用nginx作为反向代理抗住压力,apache作为后端处理动态请求。