nginx的基本配置
0、location后的参数只作为标识,不占位
1)访问https://www.viuman.com指向微服务
location / { proxy_pass http://localhost:8080; }
2)访问https://www.viuman.com/validate/apple-app-site-association指向/usr/local/nginx/html/validate/apple-app-site-association
location /validate {
root html;
}
1、通过端口区分不同虚拟机
server { #一个Server节点就是一虚拟主机 listen 80; server_name localhost; location / { root html; #Html是nginx安装目录下的html目录 index index.html index.htm; } } server { listen 81; server_name localhost; location / { root html; index index.html index.htm; } }
2、通过域名区分不同虚拟机
server { listen 80; server_name www.taobao.com; location / { root html-taobao; index index.html index.htm; } } server { listen 80; server_name www.baidu.com; location / { root html-baidu; index index.html index.htm; } }
3、配置成反向代理服务器
第一步:安装两个tomcat,分别运行在8080和8081端口。
第二步:启动两个tomcat。
第三步:反向代理服务器的配置(将请求转发给tomcat服务器):
upstream tomcat1 { server 192.168.25.148:8080; } server { listen 80; server_name www.sina.com.cn; location / { proxy_pass http://tomcat1; #转发到upstream为tomcat1的web服务器 index index.html index.htm; } } upstream tomcat2 { server 192.168.25.148:8081; } server { listen 80; server_name www.sohu.com; location / { proxy_pass http://tomcat2; index index.html index.htm; } }
第四步:nginx重新加载配置文件
第五步:配置域名
在hosts文件中添加域名和ip的映射关系
192.168.25.148 www.sina.com.cn 192.168.25.148 www.sohu.com
5、配置负载均衡
如果一个服务由多台服务器提供,需要把负载分配到不同的服务器处理,需要负载均衡。
upstream tomcat2 { server 192.168.25.148:8081; server 192.168.25.148:8082; }
可以根据服务器的实际情况调整服务器权重。权重越高分配的请求越多,权重越低,请求越少。默认是都是1
upstream tomcat2 { server 192.168.25.148:8081; server 192.168.25.148:8082 weight=2; }
6、配置密码认证
1.安装apache密码生产工具
yum install httpd-tools
2.生成密码文件
htpasswd -c -b /usr/local/nginx/conf/kibana.passwd bofeng 123456
3.修改nginx.conf
location / { auth_basic "Kibana Auth"; auth_basic_user_file /usr/local/nginx/conf/kibana.passwd; proxy_pass http://172.16.160.66:5601; }
其中auth_basic、auth_basic_user_file可加在和location同级
4.重启nginx