原文地址:https://blog.csdn.net/qq_22211217/article/details/80686622
https://blog.csdn.net/liuhailiuhai12/article/details/75009661/
https://blog.csdn.net/cheng_kohui/article/details/82930464
https://www.cnblogs.com/onmyway20xx/p/3666044.html
配置server_name为子定义域名时需要设置hosts文件
例:window下c:\windows\system32\drivers\etc 中的hosts文件中,加入所需的信息
118.126.100.138 www.zkh.com
118.126.100.138 www.zkh.org
118.126.100.138 zkh.com
118.126.100.138 zkh.org
一 Nginx作用
1 HTTP服务器
2 反向代理
3 负载均衡
4 正向代理
5 虚拟主机、等
二 Nginx安装
1 下载nginx最新稳定版(windows)nginx-1.14.0
2 解压
备注: 如上图,配置关键配置文件后,双击negix.exe即可。注意默认端口80,请注意别冲突
3 关键配置简单介绍
conf文件夹下nginx.conf
#user nobody;
#开启进程数 <=CPU数
worker_processes 1;
#错误日志保存位置
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#进程号保存文件
#pid logs/nginx.pid;
#每个进程最大连接数(最大连接=连接数x进程数)每个worker允许同时产生多少个链接,默认1024
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;
#设定请求缓冲
#client_header_buffer_size 1k;
#large_client_header_buffers 4 4k;
#设定负载均衡的服务器列表
#upstream myproject {
#weigth参数表示权值,权值越高被分配到的几率越大
#max_fails 当有#max_fails个请求失败,就表示后端的服务器不可用,默认为1,将其设置为0可以关闭检查
#fail_timeout 在以后的#fail_timeout时间内nginx不会再把请求发往已检查出标记为不可用的服务器
#}
#webapp
#upstream myapp {
# server 192.168.1.171:8080 weight=1 max_fails=2 fail_timeout=30s;
# server 192.168.1.172:8080 weight=1 max_fails=2 fail_timeout=30s;
#}
#配置虚拟主机,基于域名、ip和端口
server {
#监听端口
listen 80;
#监听域名
server_name localhost;
#charset koi8-r;
#nginx访问日志放在logs/host.access.log下,并且使用main格式(还可以自定义格式)
#access_log logs/host.access.log main;
#返回的相应文件地址
location / {
#设置客户端真实ip地址
#proxy_set_header X-real-ip $remote_addr;
#负载均衡反向代理
#proxy_pass http://myapp;
#返回根路径地址(相对路径:相对于/usr/local/nginx/)
root html;
#默认访问文件
index index.html index.htm;
}
#配置反向代理tomcat服务器:拦截.jsp结尾的请求转向到tomcat
#location ~ \.jsp$ {
# proxy_pass http://192.168.1.171:8080;
#}
#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;
#}
}
#虚拟主机配置:
server {
listen 1234;
server_name bhz.com;
location / {
#正则表达式匹配uri方式:在/usr/local/nginx/bhz.com下 建立一个test123.html 然后使用正则匹配
#location ~ test {
## 重写语法:if return (条件 = ~ ~*)
#if ($remote_addr = 192.168.1.200) {
# return 401;
#}
#if ($http_user_agent ~* firefox) {
# rewrite ^.*$ /firefox.html;
# break;
#}
root bhz.com;
index index.html;
}
#location /goods {
# rewrite "goods-(\d{1,5})\.html" /goods-ctrl.html;
# root bhz.com;
# index index.html;
#}
#配置访问日志
access_log logs/bhz.com.access.log main;
}
# 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简单配置
1 HTTP服务器
Nginx本身也是一个静态资源服务器,所以nginx可以处理部分静态资源请求,实现动态资源请求和静态资源请求分离。
(1)基本配置
server {
listen 80; #监听端口
server_name localhost;#negix服务名
location / {
root e:\nginx; #站点根目录
index index.html; #首页 按出现顺序检查加载
}
}
备注:登录http://localhost:80 nginx如tomcat默认加载 root目录下文件 即index.html
(2)动静分离配置
server {
listen 80; #监听端口
server_name localhost;#negix服务名
location / {
root e:\nginx; #站点根目录
index index.html; #首页 按出现顺序检查加载
}
# 正则表达式匹配静态请求,都由nginx代理到nginx指定的静态目录中
location ~ \.(gif|jpg|jpeg|png|bmp|swf|css|js)$ {
root e:\nginx\static;
}
# 正则表达式匹配所有动态请求,都转发给tomcat处理
location ~ \.(jsp|do)$ {
proxy_pass http://ip:port/wabappname;
}
#正则匹配错误页由nginx指定到nginx错误页面存放路径
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root e:\nginx\error;
}
}
备注: 正则表达式详细参考nginx正则表达式详解
2 反向代理
Nginx与web服务集群关系就像是web服务的代理人,请求经过nginx代理转发到目标web服务器。
server {
listen 80; #监听端口
server_name www.site.com;#negix服务名
location / {
root e:\nginx; #站点根目录
index index.html; #首页 按出现顺序检查加载
proxy_pass http://192.168.1.62:8080; # 应用服务器HTTP地址
}
}
3 负载均衡
当网站访问量上来,单台web服务器无法支撑需要扩展时,nginx可以通过反向代理将请求代理到多态web服务器
upstream myapp {
server 192.168.1.111:8080; # web服务器1
server 192.168.1.112:8080; # web服务器2
}
server {
listen 80; #监听端口
server_name www.site.com; #negix服务名
location / {
root e:\nginx; #站点根目录
index index.html; #首页 按出现顺序检查加载
proxy_pass http://myapp; #应用服务器HTTP地址 用upstream 的名字代替
}
}
负载均衡核心代码为
upstream myapp {
server 192.168.1.111:8080; # web服务器1
server 192.168.1.112:8080; # web服务器2
}
默认为轮询策略 即nginx根据请求时间交替转发到两台web服务器
(1)RR轮询
如上
(2)weight权重
upstream myapp {
server 192.168.1.111:8080 weight=9; # web服务器1
server 192.168.1.112:8080 weight=1; # web服务器2
}
备注:web服务器1访问概率90% web服务器2访问概率10%
(3)iphash
upstream myapp {
ip_hash;
server 192.168.1.111:8080; # web服务器1
server 192.168.1.112:8080; # web服务器2
}
备注:客户端根据ip的hash运算会指定到某一台机器上,优点:session不存在跨服务器,缺点:用户指定服务器故障则不可达
(4)urlhash
upstream myapp {
hash $request_uri;
hash_method crc32; # 指定hash算法
server 192.168.1.111:8080; # web服务器1
server 192.168.1.112:8080; # web服务器2
}
备注:同一请求的hash指定到某一台机器上,优点:若缓存则不存在跨服务器,缺点:热点流量容易拖垮服务器,第三方支持。
(5)fair
upstream myapp {
fair;
server 192.168.1.111:8080; # web服务器1
server 192.168.1.112:8080; # web服务器2
}
备注:根据服务器反应时间取最短响应服务器。 优点:性能最大化,缺点:第三方支持。
4 正向代理
常用于内网用户通过nginx正向代理到其他dns服务
server {
resolver 1.1.1.1 8.8.8.8;
listen 80;
location /
{
proxy_pass http://$http_host$request_uri;
}
}
备注:nginx代理dns服务器由 resolver指定 多个用空格隔开
5 虚拟主机
与负载均衡用途相反,常用于将低流量web应用服务器定制不同访问域名以及相同的ip,以整合服务器资源。
server {
listen 80 default_server;
server_name _;
return 444; # 过滤其他域名的请求,返回444状态码
}
server {
listen 80;
server_name www.aaa.com; # www.aaa.com域名
location / {
proxy_pass http://localhost:8080; # 对应端口号8080
}
}
server {
listen 80;
server_name www.bbb.com; # www.bbb.com域名
location / {
proxy_pass http://localhost:8081; # 对应端口号8081
}
}
备注:如上配置 即在主机上配置了两个web应用以及其对应的访问域名。