nginx基本使用
一、nginx的四大版本
1.nginx开源版 : http://nginx.org/
2.Nginx plus商业版: https://www.nginx.com
3.Openresty: http://openresty.org
4.Tengine: http://tengine.taobao.org/
二、开源版本下载安装
centos7 下载nginx
wget http://nginx.org/download/nginx-1.21.6.tar.gz
解压 nginx
tar -zxvf nginx-1.21.6.tar.gz
如果没有gcc还需安装gcc
yum install gcc
yum install gcc-c++
安装openssl
yum install -y openssl openssl-devel
安装perl库
yum install -y pcre pcre-devel
安装zlib库
yum install -y zlib zlib-devel
指定nginx安装目录
进入解压后的 nginx-1.21.6目录,会发现有个configure脚本,我们利用它来安装,这个脚本也可以用来检查安装nginx缺失的依赖,
./configure --prefix=/usr/local/nginx
编译
make
make install
编译完后就可以在/usr/local/目录发现nginx
进入 sbin,启动nginx
nginx启动停止命令
./nginx 启动
./nginx -s stop 立即停止
./nginx -s quit 优雅停止,在关闭前完成已经接受的请求
./nginx -reload 重新加载配置,无需停止nginx
三、nginx配置文件简单介绍
/usr/local/nginx/conf/nginx.conf
worker_processes 1; #开启的进程数
events {
worker_connections 1024; #每个进程可以创建多少个连接
}
http {
#mime.types 为文件类型映射,会在http请求头里标明返回或者发送的文件是什么类型,比如是文本还是html文档,
#这是由服务器端(nginx)来定义的,如果你有特殊的文件类型,也可以在mime.types里自定义。
include mime.types; # include:引入其他配置文件
default_type application/octet-stream; #默认类型,如果不包含在mime.types里 ,就会以这种类型返回
sendfile on; #开启零拷贝
keepalive_timeout 65; #保持连接的超时时间
#一个server代表一个主机
server {
listen 80; #监听端口号
server_name localhost; #域名,主机名
location / {
root html; #相对路径,对应nginx目录下的html目录
index index.html index.htm; #访问当前location时,如果存在这些页面,则默认展示。
}
#服务器内部错误的时候 访问的页面
#500 502 503 504 会转向/50x.html页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
proxy_pass
proxy_pass 和(root,index)只能使用一个
直接使用地址,当访问时会直接访问到指定地址
location / {
proxy_pass http://www.baidu.com;
}
注意:proxy_pass 不支持https
负载均衡
基本配置
我们有两台服务器101和102,我们需要给代理的域名起个别名,然后再用
proxy_pass 后跟上别名即可 如下:
#...省略上面
upstream loadbalance {
server 192.168.100.101:80;
server 192.168.100.102:80;
server 192.168.100.103:80;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://loadbalance;
}
#...省略其余内容
}
权重weght
upstream loadbalance {
server 192.168.100.101:80 weight=1;
server 192.168.100.102:80 weight=2;
server 192.168.100.103:80 weight=3;
}
下线down
代表101不参与负载均衡
upstream loadbalance {
server 192.168.100.101:80 weight=1 down;
server 192.168.100.102:80 weight=2;
server 192.168.100.103:80 weight=3;
}
备用机backup
代表103作为备用机,其他机器都没法工作时才使用
upstream loadbalance {
server 192.168.100.101:80 weight=1 down;
server 192.168.100.102:80 weight=2;
server 192.168.100.103:80 weight=3 backup;
}
负载均算法:
weight 轮询:无法保持会话
ip_hash
:当nginx接收请求,nginx决定把请求发给哪个tomcat,它会拿到客户端到ip,进行哈希运算,再取模tomcat数量。
作用:保持会话
缺点:这只是才同一台机器上保持会话,并没有完全实现保持会话的功能。
url_hash
:根据url hash,适用于固定资源不在同一服务器上的情况
least_conn
: 最少连接的机器优先使用
least_time
: 最小相应时间,取相应最快ip分配更高的权重
动静分离
将 动态数据
和 静态资源(图片,js,css等)
分离开,动态数据让tomcat返回,静态数据前置到nginx中。
location / {
proxy_pass http://ip:port;
}
location /css {
root html;
index index.html;
}
上述配置,css目录需放在html目录下,其他js image等资源同理。接着如果请求访问css资源时,会直接访问nginx本身的静态资源
注意:/css
优先级大于/
URL Rewrite
location / {
rewrite ^/([0-9]+).html$ /index.jsp?pageNum=$1 break;
proxy_pass http://ip:port;
}
距离:上述配置 当你访问 5.html
时等价于访问/index.jsp?pageNum=5
,break代表匹配到这条规则后即可退出
防盗链
当通过http请求A进入一个页面,这个页面内部要需要调用其他http请求B的时候,http请求B的header中会带有refer字段,代表该请求来自哪里。防盗链可以限制请求的来源。
location /css {
valid_referers none 192.168.100.101;
if($invalid_referer){
return 403;
}
root html;
index index.html;
}
none代表不带refer时依然可以访问
自定制错误跳转页面
error_page 404 /404.html;
location = /404.html {
root html;
}
也可以在return的时候之后直接返回页面
if($invalid_referer){
return 404.html;
}