目录
一.Nginx基础概念
高性能轻量级WEB服务器NGINX
1.优缺点比较
1.Nginx
- 一款高性能、轻量级Web服务软件。比apache占用更少的内存和资源
- 对HTTP并发连接的处理能力高。nginx处理请求是异步非阻塞。
- 单台物理服务器可支持30000~50000个并发请求
2.apache
- Apache是以进程为基础的结构
- rewrite比nginx的rewrite强大。rewrite的主要功能就是实现统一资源定位符(URL)的跳转
- 模块多
- 少bug
- 超稳定
需要性能的web服务,用nginx。若不需要性能只求稳定,就选用apache。
nginx配置简洁, apache较复杂
3.总结
-
apache是同步多进程模型,一个连接对应一个进程,nginx是异步的,多个连接可以对应一个进程。
-
Nginx处理静态文件好,耗费内存少,只适合静态和反向。
-
Apache在处理动态有优势,
-
nginx并发性比较好,CPU占用内存低,如果rewrite频繁,选用apache最佳。
-
总的来说,apache依然是大部分公司的首选。
2.apache三种工作模式
1.prefork
2.work
3.event
3. Linux的I/O
I/O就是把数据从内核空间中的内存数据复制到用户空间中进程的内存当中。
1.I/O类型
-
磁盘I/O:buff/cache 的区别
-
网络I/O:一切皆文件,本质为对socket文件的读写
磁盘I/O:
网络I/O:
2.同步和异步
-
同步: synchronous,被调用者并不提供事件的处理结果相关的通知消息,需要调用者主动询问事情是否处理完成
-
异步: asynchronous,被调用者通过状态、通知或回调机制主动通知调用者被调用者的运行状态
3. 阻塞和非阻塞
-
阻塞: blocking,指IO操作需要彻底完成后才返回到用户空间,调用结果返回之前,调用者被挂起,干不了别的事情。
-
非阻塞: nonblocking,指IO操作被调用后立即返回给用户一个状态值,而无需等到IO操作彻底完成,在最终的调用结果返回之前,调用者不会被挂起,可以去做别的事情。
二.编译安装Nginx服务
1.上传安装包
百度网盘自取
链接:https://pan.baidu.com/s/1iGH3Mep3lw2UHdbBjoUg4Q
提取码:ebmn
2.安装依赖关系包
[root@localhost opt]# yum -y install pcre-devel zlib-devel gcc gcc-c++ make
3.新建用户和组便于管理
nginx 服务程序默认 以 nobody 身份运行,建议为其创建专门的用户账户,以便更准确的控制访问权限
[root@localhost opt]# useradd -M -s /sbin/nologin nginx
[root@localhost opt]# id nginx
uid=1001(nginx) gid=1001(nginx) 组=1001(nginx)
4.编译安装Nginx
[root@localhost opt]cd nginx-1.12.0/
[root@localhost nginx-1.12.0]# ./configure \
--prefix=/usr/local/nginx \ #指定nginx的安装路径
--user=nginx \ #指定用户名
--group=nginx \ #指定组名
--with-http_stub_status_module #启用 http_stub_status_module 模块以支持状态统计
[root@localhost nginx-1.12.0]# make -j4 && make install
[root@localhost nginx-1.12.0]# cd /usr/local/nginx/
[root@localhost nginx]# ls
conf html logs sbin
[root@localhost nginx]# cd sbin/
[root@localhost sbin]# ls
nginx
[root@localhost sbin]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost sbin]# nginx
[root@localhost sbin]# ss -ntap |grep 80
LISTEN 0 128 *:80 *:* users:(("nginx",pid=6091,fd=6),("nginx",pid=6090,fd=6))
[root@localhost sbin]# cd ..
[root@localhost nginx]# ls
client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp
[root@localhost nginx]# cd logs/
[root@localhost logs]# ls
access.log error.log nginx.pid
[root@localhost logs]# cat nginx.pid
6090
[root@localhost logs]# kill -3 6090
[root@localhost logs]# ss -ntap |grep 80
1.查看进程号的几种方法
[root@localhost logs]# ls
access.log error.log nginx.pid
[root@localhost logs]# cat nginx.pid
6166
[root@localhost logs]# ps aux |grep nginx
root 6166 0.0 0.0 20496 600 ? Ss 14:52 0:00 nginx: master process nginx
nginx 6167 0.0 0.0 23024 1388 ? S 14:52 0:00 nginx: worker process
root 6198 0.0 0.0 112676 984 pts/0 S+ 14:54 0:00 grep --color=auto nginx
[root@localhost logs]# ss -ntap |grep 80
LISTEN 0 128 *:80 *:* users:(("nginx",pid=6167,fd=6),("nginx",pid=6166,fd=6))
[root@localhost logs]# lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 6166 root 6u IPv4 40307 0t0 TCP *:http (LISTEN)
nginx 6167 nginx 6u IPv4 40307 0t0 TCP *:http (LISTEN)
5.添加Nginx服务
法一
[root@localhost init.d]# vim nginx //不需要加.sh
[root@localhost ~]# cd /etc/init.d/
[root@localhost init.d]# ls
functions netconsole network README
[root@localhost init.d]# vim nginx
#!/bin/bash
#chkconfig:35 99 20
#description:Nginx Service Control Script
cmd="/usr/local/nginx/sbin/nginx"
pid="/usr/local/nginx/logs/nginx.pid"
case $1 in
start)
$cmd
;;
stop)
kill -3 `cat $pid`
;;
restart)
$0 stop
$0 start
;;
reload)
kill -1 `cat $pid`
;;
*)
echo "please input start stop restart reload"
exit 0
esac
exit 1
[root@localhost init.d]# chmod +x nginx
[root@localhost init.d]# chkconfig --add nginx
[root@localhost init.d]# ss -ntap |grep 80
[root@localhost init.d]# service nginx start
[root@localhost init.d]# service nginx start
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 init.d]# ss -ntap |grep 80
LISTEN 0 128 *:80 *:* users:(("nginx",pid=6618,fd=6),("nginx",pid=6617,fd=6))
[root@localhost init.d]# service nginx stop
[root@localhost init.d]# ss -ntap |grep 80
[root@localhost init.d]# service nginx start111
please input start stop restart reload
[root@localhost init.d]# service nginx start
[root@localhost init.d]# ss -ntap |grep 80
LISTEN 0 128 *:80 *:* users:(("nginx",pid=6839,fd=6),("nginx",pid=6838,fd=6))
法二
[root@localhost system]# vim nginx.service
[root@localhost ~]# cd /lib/systemd/system
[root@localhost system]# vim 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/bin/kill -s HUP $MAINPID
ExecStop=/usr/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@localhost system]# systemctl daemon-reload
[root@localhost system]# ss -ntap |grep 80
[root@localhost system]# systemctl start nginx
[root@localhost system]# ss -ntap |grep 80
LISTEN 0 128 *:80 *:* users:(("nginx",pid=7999,fd=6),("nginx",pid=7998,fd=6))
[root@localhost system]# 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 system]# nginx -v
nginx version: nginx/1.12.0
5.配置文件
由各种配置语句组成,不使用特定的界定标记。全局配置部分包括 Nginx 服务的运行 用户、工作进程数、错误日志、PID 存放位置等基本设置
1.全局配置
2.I/O事件配置
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;
}
}
}
/usr/local/nginx/conf
[root@localhost conf]# vim nginx.conf
windows中
linux中
[root@localhost conf]# vim nginx.conf
[root@localhost html]# vim test/index.html
如果是root 就表示 /test 就代表/usr/local/nginx/html/test/ 目录下的文件
是alias 就表示 就是/usr/local/nginx/html/test.html
4.日志格式设定
-
$remote_addr与$http_x_forwarded_for用以记录客户端的ip地址;
-
$remote_user:用来记录客户端用户名称;
-
$time_local:用来记录访问时间与时区;
-
$request:用来记录请求的url与http协议;
-
$status:用来记录请求状态:成功是200,300,400,500
-
$body bytes_sent :记录发送给客户端文件主体内容大小;
-
$http_referer:用来记录从哪个页面链接访问过来的;
-
$http_user_agent:记录客户浏览器的相关信息;
5.访问状态统计配置
[root@localhost html]# vim test/index.html
6.基于授权密码的访问控制
生成用户密码认证文件
[root@localhost conf]# vim nginx.conf
[root@localhost conf]# yum install -y httpd-tools.x86_64
[root@localhost conf]# htpasswd -c /usr/local/nginx/passwd.db ooo
New password:
Re-type new password:
Adding password for user ooo
[root@localhost conf]# htpasswd /usr/local/nginx/passwd.db ouyy
New password:
Re-type new password:
Adding password for user ouyy
[root@localhost conf]# cat /usr/local/nginx/passwd.db
ooo:$apr1$QZOlYHnC$5ulQ5HjiEaG9k/3mRygC4/
ouyy:$apr1$ky60OwvJ$WGqgN0SupLeFKnxK/rC970
[root@localhost conf]# vim nginx.conf
44 location / {
45 auth_basic "secret";
46 auth_basic_user_file /usr/local/nginx/passwd.db;
47 root html;
48 index index.html index.htm;
49 }
[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.service
7.基于客户端的访问控制
[root@localhost conf]# vim nginx.conf
[root@localhost conf]# vim nginx.conf
44 location / {
45 deny 192.168.68.139;
46 allow all;
47 #auth_basic "secret";
48 #auth_basic_user_file /usr/local/nginx/passwd.db;
49 root html;
50 index index.html index.htm;
51 }
8.基于域名的nginx 虚拟主机
[root@localhost conf]# vim nginx.conf
[root@localhost conf]# cd /var/
[root@localhost var]# ls
account cache db games kerberos local log nis preserve spool tmp
adm crash empty gopher lib lock mail opt run target yp
[root@localhost var]# mkdir -p www/html/{ooo1,ooo2}
[root@localhost var]# ls www/html/
ooo1 ooo2
[root@localhost var]# echo "hello ooo1" >/var/www/html/ooo1/index.html
[root@localhost var]# echo "hello ooo2" >/var/www/html/ooo2/index.html
[root@localhost var]# cat /var/www/html/ooo1/index.html
hello ooo1
[root@localhost var]# cat /var/www/html/ooo2/index.html
hello ooo2
[root@localhost conf]# vim nginx.conf
36 server {
37 listen 80;
38 server_name www.ooo1.com;
39
40 charset utf-8;
41
42 access_log logs/www.ooo1.access.log;
43
44 location / {
45 root /var/www/html/ooo1;
46 index index.html index.htm;
47 }
48 #error_page 404 /404.html;
49
50 # redirect server error pages to the static page /50x.html
51 #
52 error_page 500 502 503 504 /50x.html;
53 location = /50x.html {
54 root html;
55 }
56 }
57 server {
58 listen 80;
59 server_name www.ooo2.com;
60
61 charset utf-8;
62
63 access_log logs/www.ooo2.access.log;
64
65 location / {
66 root /var/www/html/ooo2;
67 index index.html index.htm;
68 }
69 #error_page 404 /404.html;
70
71 # redirect server error pages to the static page /50x.html
72 #
73 error_page 500 502 503 504 /50x.html;
74 location = /50x.html {
75 root html;
76 }
77 }
[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.service
[root@localhost ~]# vim /etc/hosts
此时的域名是无法使用的,所以需要修改/etc/hosts文件
9.基于IP地址
[root@localhost conf]# vim nginx.conf
[root@localhost conf]# ifconfig ens33:0 192.168.68.123
[root@localhost conf]# ifconfig ens33:0
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.68.123 netmask 255.255.255.0 broadcast 192.168.68.255
ether 00:0c:29:25:38:11 txqueuelen 1000 (Ethernet)
[root@localhost conf]# vim nginx.conf
36 server {
37 listen 192.168.68.123:80;
38 server_name www.ooo1.com;
39
40 charset utf-8;
41
42 access_log logs/www.ooo1.access.log;
43
44 location / {
45 allow all;
46 root /var/www/html/ooo1;
47 index index.html index.htm;
48 }
49 #error_page 404 /404.html;
50
51 # redirect server error pages to the static page /50x.html
52 #
53 error_page 500 502 503 504 /50x.html;
54 location = /50x.html {
55 root html;
56 }
57 }
58 server {
59 listen 192.168.68.30:80;
60 server_name www.ooo2.com;
61
62 charset utf-8;
63
64 access_log logs/www.ooo2.access.log;
65
66 location / {
67 allow all;
68 root /var/www/html/ooo2;
69 index index.html index.htm;
70 }
71 #error_page 404 /404.html;
72
73 # redirect server error pages to the static page /50x.html
74 #
75 error_page 500 502 503 504 /50x.html;
76 location = /50x.html {
77 root html;
78 }
79 }
80 }
[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.service
10.基于端口
[root@localhost conf]# vim nginx.conf
[root@localhost conf]# vim nginx.conf
36 server {
37 listen 192.168.68.123:8080;
38 server_name www.ooo1.com;
39
40 charset utf-8;
41
42 access_log logs/www.ooo1.access.log;
43
44 location / {
45 allow all;
46 root /var/www/html/ooo1;
47 index index.html index.htm;
48 }
49 #error_page 404 /404.html;
50
51 # redirect server error pages to the static page /50x.html
52 #
53 error_page 500 502 503 504 /50x.html;
54 location = /50x.html {
55 root html;
56 }
57 }
58 server {
59 listen 192.168.68.30:234;
60 server_name www.ooo2.com;
61
62 charset utf-8;
63
64 access_log logs/www.ooo2.access.log;
65
66 location / {
67 allow all;
68 root /var/www/html/ooo2;
69 index index.html index.htm;
70 }
71 #error_page 404 /404.html;
72
73 # redirect server error pages to the static page /50x.html
74 #
75 error_page 500 502 503 504 /50x.html;
76 location = /50x.html {
77 root html;
78 }
79 }
80 }
[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.service