目录
1、Nginx学习(一)——Nginx基础知识
2、Nginx学习(二)——配置文件、反向代理与负载均衡
3、Nginx搭建HTTPS服务器
一、初试Nginx
一.下载安装
1、安装必要的一些类库和软件支持(centos)
centos平台编译环境使用如下指令
yum -y install gcc automake autoconf libtool make
yum install gcc gcc-c++
正则表达相关PCRE
centos:
yum install -y pcre pcre-devel
yum -y install openssl openssl-devel
安装zlib库
wget https://nchc.dl.sourceforge.net/project/libpng/zlib/1.2.11/zlib-1.2.11.tar.gz
解压
tar -zxf zlib-1.2.11.tar.gz
进入目录
cd zlib-1.2.11
检测环境
./configure
编译
make
安装
make install
2、下载
链接
http://nginx.org/en/download.html
右键复制链接地址,然后下载
wget http://nginx.org/download/nginx-1.20.0.tar.gz
下载之后进行解压
tar -zxf nginx-1.20.0.tar.gz
解压之后打开nginx解压后的文件夹
drwxr-xr-x 6 yang yang 4096 Apr 22 21:52 auto
-rw-r--r-- 1 yang yang 311102 Apr 20 21:35 CHANGES
-rw-r--r-- 1 yang yang 474697 Apr 20 21:35 CHANGES.ru
drwxr-xr-x 2 yang yang 4096 Apr 22 21:52 conf
-rwxr-xr-x 1 yang yang 2590 Apr 20 21:35 configure
drwxr-xr-x 4 yang yang 4096 Apr 22 21:52 contrib
drwxr-xr-x 2 yang yang 4096 Apr 22 21:52 html
-rw-r--r-- 1 yang yang 1397 Apr 20 21:35 LICENSE
drwxr-xr-x 2 yang yang 4096 Apr 22 21:52 man
-rw-r--r-- 1 yang yang 49 Apr 20 21:35 README
drwxr-xr-x 9 yang yang 4096 Apr 22 21:52 src
1.auto目录(为了辅助nginx的config文件)
drwxr-xr-x 2 yang yang 4096 Apr 22 21:52 cc //编译用
-rw-r--r-- 1 yang yang 141 Apr 20 21:35 define
-rw-r--r-- 1 yang yang 889 Apr 20 21:35 endianness
-rw-r--r-- 1 yang yang 2812 Apr 20 21:35 feature
-rw-r--r-- 1 yang yang 136 Apr 20 21:35 have
-rw-r--r-- 1 yang yang 137 Apr 20 21:35 have_headers
-rw-r--r-- 1 yang yang 411 Apr 20 21:35 headers
-rw-r--r-- 1 yang yang 1020 Apr 20 21:35 include
-rw-r--r-- 1 yang yang 768 Apr 20 21:35 init
-rw-r--r-- 1 yang yang 4875 Apr 20 21:35 install
drwxr-xr-x 11 yang yang 4096 Apr 22 21:52 lib //lib库
-rw-r--r-- 1 yang yang 18324 Apr 20 21:35 make
-rw-r--r-- 1 yang yang 3934 Apr 20 21:35 module
-rw-r--r-- 1 yang yang 38640 Apr 20 21:35 modules
-rw-r--r-- 1 yang yang 136 Apr 20 21:35 nohave
-rw-r--r-- 1 yang yang 25469 Apr 20 21:35 options
drwxr-xr-x 2 yang yang 4096 Apr 22 21:52 os //对操作系统的判断
-rw-r--r-- 1 yang yang 8752 Apr 20 21:35 sources
-rw-r--r-- 1 yang yang 120 Apr 20 21:35 stubs
-rw-r--r-- 1 yang yang 2014 Apr 20 21:35 summary
-rw-r--r-- 1 yang yang 394 Apr 20 21:35 threads
drwxr-xr-x 2 yang yang 4096 Apr 22 21:52 types
-rw-r--r-- 1 yang yang 26166 Apr 20 21:35 unix
2.CHANGES文件
修复了那些BUG和提供了哪些新的特性
3.CHANGES.ru文件
原作者是俄罗斯人,俄罗斯版本changes文件。
4.conf文件
设定文件
5.configure脚本
6.contrib
7.html目录
提供了两个标准的HTML,报错和欢迎。
8.LICENSE
许可证
9.man
帮助文件
10.README
自述文件
11.src
源码目录
drwxr-xr-x 2 yang yang 4096 Apr 22 21:52 core
drwxr-xr-x 3 yang yang 4096 Apr 22 21:52 event
drwxr-xr-x 4 yang yang 4096 Apr 22 21:52 http
drwxr-xr-x 2 yang yang 4096 Apr 22 21:52 mail
drwxr-xr-x 2 yang yang 4096 Apr 22 21:52 misc
drwxr-xr-x 3 yang yang 4096 Apr 22 21:52 os
drwxr-xr-x 2 yang yang 4096 Apr 20 21:35 stream
(11).objs
objs是执行./configure 后生成的文件
drwxr-xr-x 3 root root 4096 Apr 23 10:18 objs
3、安装
检测环境
./configure
编译
make
安装
make install
4、添加环境变量
编辑配置文件
vim /etc/profile
添加
PATH=$PATH:/usr/local/nginx/sbin
export PATH
重新加载配置文件即可生效
source /etc/profile
5、启动
在该目录下
/usr/local/nginx/sbin
执行
./nginx
二.Nginx指令
- 配置文件由指令和指令块组成
- 每条指令以;分号结尾,指令与参数之间以空格符号分隔
- 指令块以{ }大括号将多条指令组织在一起
- include语句允许组合多个配置文件以提升可维护性
- #符号添加注释
- $符号使用变量
- 部分参数指令支持正则表达
1、Nginx的 HTTP 配置
Global: nginx 运行相关
Events: 与用户的网络连接相关
http
http Global: 代理,缓存,日志,以及第三方模块的配置
server
server Global: 虚拟主机相关
location: 地址定向,数据缓存,应答控制,以及第三方模块的配置
2、Nginx命令行
1.格式:nginx -s(指令) reload(参数)
2.帮助指令:nginx -?或者nginx -h
3.使用指定配置文件:-c
4.使用指定配置指令:-g
5.指定运行目录: -p
6.发送信号:-s
- stop立即停止服务
- quit优雅的停止服务
- reload重载配置文件
- reopen重新开始记录日志文件
7.测试配置文件是否有语法错误:nginx -t或nginx -T
8.版本信息:nginx -v(仅仅Nigix版本),-V(带环境信息)
三.Nginx搭建一个静态资源WEB服务器
1、编辑Nginx配置文件
vim /usr/local/nginx/conf/nginx.conf
#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;
events {
worker_connections 1024;
}
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 on;
server {
#serv
#监听使用端口
listen 9080;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#location /表示所有请求
location / {
#指定访问路径。
alias /demo/nginx-demo/templates/;
#root html;
#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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# 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 html;
# index index.html index.htm;
# }
#}
}
修改完之后要重新加载配置文件
nginx -s reload
2、autoindex:目录浏览功能
Nginx默认是不允许列出整个目录的。如需此功能,打开nginx.conf文件,在location server 或 http段中加入
autoindex on;
另外两个参数最好也加上去:
autoindex_exact_size off;
默认为on,显示出文件的确切大小,单位是bytes。
改为off后,显示出文件的大概大小,单位是kB或者MB或者GB
autoindex_localtime on;
默认为off,显示的文件时间为GMT时间。
改为on后,显示的文件时间为文件的服务器时间
location / {
alias /demo/nginx-demo/templates/;
autoindex on;
# root html;
# index index.html index.htm;
}
3、gzip:压缩
gzip on; 开启压缩
gzip_min_length 1k; 设置压缩最小单位,小于不压缩
#gzip_disable "msie6";
# gzip_vary on;#和http头有关系,加个vary头,给代理服务器用的,有的浏览器支持压缩,有的不支持,所以避免浪费不支持的也压缩,所以根据客户端的HTTP头来判断,是否需要压缩
# gzip_proxied any;
gzip_comp_level 4; #压缩比,压缩等级
gzip_buffers 4 16k; # 设置系统获取几个单位的缓存用于存储gzip的压缩结果数据流
gzip_http_version 1.1; #就是说对HTTP/1.1协议的请求才会进行gzip压缩
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; 压缩内容
gzip_proxied 默认值:off Nginx作为反向代理的时候启用,开启或者关闭后端服务器返回的结果,匹配的前提是后端服务器必须要返回包含"Via"的 header头。参考链接
- off - 关闭所有的代理结果数据的压缩
- expired - 启用压缩,如果header头中包含 “Expires” 头信息
- no-cache - 启用压缩,如果header头中包含 “Cache-Control:no-cache” 头信息
- no-store - 启用压缩,如果header头中包含 “Cache-Control:no-store” 头信息
- private - 启用压缩,如果header头中包含 “Cache-Control:private” 头信息
- no_last_modified - 启用压缩,如果header头中不包含 “Last-Modified” 头信息
- no_etag - 启用压缩 ,如果header头中不包含 “ETag” 头信息
- auth - 启用压缩 , 如果header头中包含 “Authorization” 头信息
- any - 无条件启用压缩
四.Nginx搭建简单的反向代理
1、Nginx以指定配置文件启动
小知识:Nginx以指定配置文件启动报错
2、上游服务器(业务处理服务器)
server {
listen 127.0.0.1:9080;#表示只有本机的9080端口可以访问nginx服务器
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
alias /demo/nginx-demo/templates/;
autoindex on;
# root html;
# index index.html index.htm;
}
3、反向代理服务器(业务转发服务器)
server {
listen 127.0.0.1:9080;#表示只有本机的9080端口可以访问nginx服务器
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
alias /demo/nginx-demo/templates/;
autoindex on;
# root html;
# index index.html index.htm;
}
二、Nginx架构基础
一.Nginx的进程结构
nginx是多进程结构,多进程结构设计是为了保证nginx的高可用高可靠,包含:
- master进程:也是父进程,负责worker进程的管理。
- worker进程:也是子进程,worker进程一般配置成与服务器的CPU核数相同,worker进程用来处理具体的请求的。
- cache进程:也是子进程,包括cache manager和cache loader进程,主要是反向代理时做缓存使用。
二.Nginx的进程管理:信号
三.Nginx的reload过程(打开新的之后,再去关闭旧的,做到无缝连接)
- 执行reload命令,向master进程发送HUP信号。
- master进程首先校验语法是否正确
- master进程打开新的监听端口
- master进程用新的配置启动新的work子进程
- master进程向老的进程发送QUIT信号
- 老的进程处理完当前连接后结束进程