Nginx 是一款开源的高性能 HTTP 服务器和反向代理服务器,同时支持 IMAP/POP3/SMTP 代理服务,其性能优势着为显著,官网上称:单台 Nginx 服务器可以处理 50000 并发。
Order deny,allow
。每次 I/O 时,都要经过两个阶段:
epoll
的方式,提供给用户访问,复制数据的一些操作交由内核完成;epoll
在 Linux 2.6 中增加了内存拷贝 mmap
机制,加速与内核空间的消息传递,即内存映射。
inode
,在内存中映射一个相同的 inode
,大小也相同,下次拿数据不需要遍历 inode
,分析路径了。这样提高了效率。cache miss
,获得更好的性能。零拷贝 (SendFile) 文件传输是在内核中操作完成的,函数直接在两个文件描述符之间传输数据,从而避免了内核缓冲区数据和用户缓冲区数据之间的拷贝,操作效率很高。
执行过程:
准备工作
主机名 | 操作系统 | IP 地址 | 版本号 |
---|---|---|---|
Nginx | CentOS 7.4 | 192.168.1.1 | nginx-1.18.0.tar.gz |
http://nginx.org/
[root@Nginx ~]# wget http://www.nginx.org/download/nginx-1.18.0.tar.gz
[root@Nginx ~]# yum -y install pcre-devel zlib-devel
[root@Nginx ~]# ls
anaconda-ks.cfg nginx-1.18.0.tar.gz
[root@Nginx ~]# tar zxf nginx-1.18.0.tar.gz -C /usr/src/
[root@Nginx ~]# cd /usr/src/nginx-1.18.0/
[root@Nginx nginx-1.18.0]# useradd -M -s /sbin/nologin nginx
[root@Nginx nginx-1.18.0]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module && make && make install
[root@Nginx nginx-1.18.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ #创建软链接优化执行路径
[root@Nginx nginx-1.18.0]# cd
[root@Nginx ~]# nginx -v #查看版本
[root@Nginx ~]# nginx #启动Nginx
[root@Nginx ~]# netstat -anpt | grep nginx #查看网络连接状态
[root@Nginx ~]# vim /usr/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=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@Nginx ~]# nginx -s stop
[root@Nginx ~]# netstat -anpt | grep 80
[root@Nginx ~]# systemctl start nginx
[root@Nginx ~]# netstat -anpt | grep 80
1)查看当前 CPU 物理状态
[root@Nginx ~]# lscpu | grep "CPU(s)"
2)将 Nginx Worker 进程绑到不同的核心上
worker_processes 6;
worker_cpu_affinity 000001 000010 000100 001000 010000 100000;
worker_processes auto;
worker_cpu_affinity auto;
3)查看 Nginx Worker 进程绑定至对应 CPU
[root@Nginx ~]# ps -eo pid,args,psr | grep [n]ginx
[root@Nginx ~]# mkdir -p /zhangsan/Coco{
1..3}
[root@Nginx ~]# for A in {
1..3};do echo "Coco $A
" > /zhangsan/Coco"$A"/index.html;done
[root@Nginx ~]# vim /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name 192.168.1.1;
location / {
root /zhangsan/Coco1;
index index.html;
}
}
server {
listen 80;
server_name 192.168.1.1;
location / {
root /zhangsan/Coco2;
index index.html;
}
}
server {
listen 80;
server_name 192.168.1.1;
location / {
root /zhangsan/Coco3;
index index.html;
}
}
}
[root@Nginx ~]# nginx -t #检查配置文件是否正确
[root@Nginx ~]# systemctl restart nginx #重启Nginx服务
[root@Nginx ~]# curl http://192.168.1.1
server_name
一样时,访问的优先级是从上到下。一个 Server 出现多个 location
常用的正则匹配 | 作用 |
---|---|
= |
进行普通字符精确匹配,完全匹配 |
^~ |
进行普通字符匹配,使用前缀匹配 |
~ |
区分大小写匹配 |
~* |
不区分大小写匹配 |
=
^~
~
~*
[root@Nginx ~]# vim /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name 192.168.1.1;
root /zhangsan;
index index.html;
location = /Coco1/ {
rewrite ^(.*)$ /Coco1/index.html break;
}
location ~ /Coco* {
rewrite ^(.*)$ /Coco3/index.html break;
}
location ^~ /Coco {
rewrite ^(.*)$ /Coco2/index.html break;
}
}
}
[root@Nginx ~]# systemctl restart nginx
[root@Nginx ~]# curl http://192.168.1.1/Coco1/
<h1>Coco 1</h1>
[root@Nginx ~]# curl http://192.168.1.1/Cocooo
<h1>Coco 2</h1>
[root@Nginx ~]# curl http://192.168.1.1/Coc
<h1>Coco 3</h1>
[root@Nginx ~]# vim /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name 192.168.1.1;
root /zhangsan;
index index.html;
location / {
try_files $uri $uri/ /Coco3/index.html;
}
}
}
[root@Nginx ~]# systemctl restart nginx
[root@Nginx ~]# curl http://192.168.1.1/Coco1/
<h1>Coco 1</h1>
[root@Nginx ~]# curl http://192.168.1.1/Docker/
<h1>Coco 3</h1>
执行过程:
uri
内容是否存在本地,存在则解析;/Coco3/index.html
指定路径来解析。Root 路径配置:
[root@Nginx ~]# vim /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name 192.168.1.1;
location /Coco1 {
root /zhangsan;
index index.html;
}
}
}
[root@Nginx ~]# systemctl restart nginx
[root@Nginx ~]# curl http://192.168.1.1/Coco1/
<h1>Coco 1</h1>
root路径 + location路径
也就是 /zhangsan/Coco1/
Alias 路径配置:
[root@Nginx ~]# vim /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name 192.168.1.1;
location /Docker {
alias /zhangsan/Coco1;
index index.html;
}
}
}
[root@Nginx ~]# systemctl restart nginx
[root@Nginx ~]# curl http://192.168.1.1/Docker/
<h1>Coco 1</h1>
alias
路径时,它会把原有的 location
路径代替,但是只能使用 location
配置的路径来访问。总结:root
是用来设置根目录的,而 alias
是用来重置当前文件的目录(也就是 location /目录
)
BeiDaiLi
并安装 Nginx,IP地址为:192.168.1.2[root@BeiDaiLi ~]# vim /usr/local/nginx/conf/nginx.conf
21 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
22 '$status $body_bytes_sent "$http_referer" '
23 '"$http_user_agent" "$http_x_forwarded_for"';
24
25 access_log logs/access.log main;
[root@BeiDaiLi ~]# echo "192.168.1.2
" > /usr/local/nginx/html/index.html
[root@BeiDaiLi ~]# systemctl restart nginx
配置 Nginx 主机
[root@Nginx ~]# vim /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name 192.168.1.1;
location / {
root html;
index index.html;
proxy_pass http://192.168.1.2;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
[root@Nginx ~]# echo "192.168.1.1
" > /usr/local/nginx/html/index.html
[root@Nginx ~]# systemctl restart nginx
访问 http://192.168.1.1
验证:
查看日志:
[root@BeiDaiLi ~]# tail /usr/local/nginx/logs/access.log