一、Nginx简介以及相关名词介绍
Nginx简介
Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。其特点是占有内存少,并发能力强。
相关名词介绍
- 正向代理:有着明确的访问地址,但是该地址访问不了,只能通过代理访问。(例子:你知道A有钱,但是你和A不熟,只能通过B去找A借钱再给你)
- 反向代理:只知道代理可以帮你完成该工作,但是代理访问的什么地址,你不知道。(例子:你知道网贷平台可以借钱给你,但是网贷平台的钱哪来的,你不知道)
- 负载均衡:将原来请求集中在单个服务器上的情况改为将请求分发到多个服务器上的操作。
- 动静分离:把动态资源和静态资源区分开,由不同的服务器解析。
二、Nginx下载与安装启动
Nginx下载
这里直接去Nginx官网下载就行,不做多的介绍
Nginx安装启动
- 将下载好的Nginx安装包(nginx-xx.tar.gz包)上传到指定服务器
- 解压缩nginx-xx.tar.gz包
- 进入解压缩目录,执行./configure(最好的是先使用./configure --help查看下相关构建参数,并指定基本目录所在),解决完错误后,再次执行./configure,直到没问题为止,执行完该命令后会生成Makefile文件
注:此步可能会出现如下错误提示,按照提示下载对应的依赖包即可
./configure: error: the HTTP rewrite module requires the PCRE library.
解决方法
yum search pcre 这里会找到很多名字为pcre依赖包
yum -y install pcre-devel 下载开发包就行
./configure: error: the HTTP gzip module requires the zlib library.
这里同上一步
yum search zlib
yum -y install zlib-devel
- make (会从Makefile文件中读取指令进行编译) && make install (会从Makefile文件中读取执行进行安装)
- less Makefile 查看下Nginx启动命令安装在地方了
- 进入到nginx启动命令的目录,执行启动命令(执行启动命令之前最好使用./nginx -h查看下nginx命令的相关参数用法)
cd /usr/local/nginx/sbin && ./nginx
- 验证
[root@localhost sbin]# ps -ef | grep nginx (出现了如下nginx:master和nginx:worker说明nginx启动成功了)
root 14844 1 0 16:10 ? 00:00:00 nginx: master process ./nginx
nobody 14845 14844 0 16:10 ? 00:00:00 nginx: worker process
root 14847 9660 0 16:10 pts/2 00:00:00 grep --color=auto nginx
三、Nginx的基本命令和配置文件
Nginx的基本命令
./nginx -h 查看nginx相关命令因提示太过详细此处不多介绍只介绍最基本的
./nginx 启动nginx服务
./nginx -s stop 停止nginx服务
./nginx -s reload 重加载nginx服务
Nginx配置文件(详细的请查阅官网)
配置文件为conf目录下的nginx.conf文件,该文件的大致内容为:
# 为省略的配置
#user nobody; 此配置标识运行nginx的用户或者用户组
worker_processes 1; 此配置标识允许生成的worker process数(一般和CPU核数对应)
#error_log logs/error.log;
#error_log logs/error.log notice; 此配置标识相关日志的保存地方
#error_log logs/error.log info;
#pid logs/nginx.pid; 此配置标识进程pid的存放地址
events {
worker_connections 1024; 此配置标识每个worker process最大的连接数
}
此模块是配置最频繁的模块,很重要
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 on;
server {
listen 80; 此配置标识nginx启动时监听的端口
server_name localhost; 此配置标识nginx启动时服务的ip
#charset koi8-r;
#access_log logs/host.access.log main;
location / { 此配置用于进行url匹配,并对匹配的url进行重定向
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使用简单实例
简单反向代理实例如下
该配置可实现访问80端口代理到8080的效果
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://localhost:8080/;
index index.html;
}
}
简单静态分发实例如下
该配置可实现访问http://localhost/dist/即访问html/dist/index.html静态文件的功能
server {
listen 80;
server_name localhost;
location /dist { 注意:如果不是用的代理,该dist路径必须在html(root)目录中存在
root html;
index index.html;
}
}
负载均衡简单实例如下:
nginx支持以下负载平衡机制:
1.轮询-对应用程序服务器的请求以轮询方式分发(默认)
2.最少连接-将下一个请求分配给活动连接最少的服务器,需要使用关键字(least_conn)。
3.ip-hash-哈希函数用于确定应为下一个请求选择哪个服务器(基于客户端的IP地址),需要使用关键字(ip_hash)。
http {
upstream myapp1 {
server srv1.example.com weight = 3; 此处的weight(默认为1)即权重,越大的话分配到请求的概率越高,该字段在最少连接和hash中都可以使用。
server srv2.example.com;
server srv3.example.com;
}
server {
listen 80;
location / {
proxy_pass http://myapp1;
}
}
}
location语法如下
Syntax: location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }
Default: —
Context: server, location
1. = :用于不含正则表达式的 uri 前,要求请求字符串与 uri 严格匹配,如果匹配成功,就停止继续向下搜索并立即处理该请求。
2. ~ :用于表示 uri 包含正则表达式,并且区分大小写。
3. ~*:用于表示 uri 包含正则表达式,并且不区分大小写。
4. ^~:用于不含正则表达式的 uri 前,要求 Nginx 服务器找到标识 uri 和请求字符串匹配度最高的 location 后,立即使用此 location 处理请求,而不再使用 location 块中的正则 uri 和请求字符串做匹配。
五、Nginx高可用实例(利用keepalived实现)
1.keepalived的下载安装(简单介绍一下)
- 将下载好的keepalived安装包(keepalived-2.0.20.tar.gz包)上传到指定服务器
- 解压缩keepalived-2.0.20.tar.gz包
- 进入解压缩目录,执行./configure命令(最好是先使用./configure --help查看下相关构建参数),此处我执行的是./configure --prefix=/home/lujun/keepalived。(中途会有权限问题和依赖问题比较简单此处不列举了)
- make && make install(执行编译构建命令)
- cd /home/lujun/keepalived/sbin(进去keepalived目录)
- ./keepalived -f /home/lujun/keepalived/etc/keepalived/keepalived.conf (以指定的配置文件启动keepalived,如果不指定的话默认为/etc/keepalived/keepalived.conf,你可以选择复制一份也可以选择启动时指定)
- keepalived的日志如果没有指定的话默认在/var/log/messages文件内
- ps -ef | grep keepalived命令进行验证
2.keepalived高可用的简单配置实例
- 进入/home/lujun/keepalived/etc/keepalived修改keepalived.conf
#主机
vrrp_script check_nginx { #脚本配置
script '/home/lujun/nginx/script/check_nginx.sh'
interval 1
weight 2
}
vrrp_instance VI_1 { #虚拟实例设置
state MASTER #指定为主机
interface ens33 #虚拟ip绑定的网卡名
virtual_router_id 51 #虚拟路由id(主备要保持一致)
priority 200 #权重(主机要大于备机)
advert_int 1
authentication { #指定授权方式
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.100 #指定虚拟ip
}
}
#备机
vrrp_script check_nginx { #脚本配置
script '/home/lujun/nginx/script/check_nginx.sh'
interval 1
weight 2
}
vrrp_instance VI_1 { #虚拟实例设置
state BACKUP #指定为主机
interface ens33 #虚拟ip绑定的网卡名
virtual_router_id 51 #虚拟路由id(主备要保持一致)
priority 199 #权重(主机要大于备机)
advert_int 1
authentication { #指定授权方式
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.100 #指定虚拟ip
}
}
# check_nginx.sh脚本文件代码,注意此处使用了killall命令,没有该命令的话请自行下载psmisc依赖包
#!/bin/bash
nginxProcNum=`ps -ef|grep nginx|wc -l`
if [ $nginxProcNum -lt 3 ];then
/home/lujun/nginx/sbin/nginx
sleep 2
if [ `ps -ef|grep nginx|wc -l` -lt 3 ];then
killall keepalived
fi
fi
来源:oschina
链接:https://my.oschina.net/u/3057088/blog/4254788