8

【nginx】nginx简单使用场景

 2 years ago
source link: https://holydogs.github.io/2021/09/20/%E3%80%90nginx%E3%80%91nginx%E7%AE%80%E5%8D%95%E4%BD%BF%E7%94%A8%E5%9C%BA%E6%99%AF/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

NGINX

需求1:将向本服务器8666端口的请求映射到A服务器的8699端口

无服务器A,暂时修改host使用,在host文件中添加以下内容

127.0.0.1 www.xff.xyz
假设A服务器域名为www.xff.xyz

找到配置文件,nginx/conf/nginx.conf,添加以下内容

upstream mytest{
  server www.xff.xyz:8699;
 }

 server {
  listen 8666;
  location / {
   proxy_pass http://mytest;
  }
 }

双击nginx.exe启动
结果看到未启动成功,到logs/error.log查看错误日志,发现80端口被占用,打开nginx.conf删除或注释掉nginx默认的listen 80端口部分代码,重新启动nginx
访问localhost:8666,成功看到运行在8699端口的应用


需求2:两台服务器A和B,使用8666端口代理,并做负载

修改nginx.conf内容为

upstream mytest{
  server www.xff.xyz:8699;
  server www.xff.xyz:8080;
 }

 server {
  listen 8666;
  location / {
   proxy_pass http://mytest;
  }
 }

重加载nginx,在cmd里输入nginx -s reload运行
(注意,8080端口在跑应用,但只是空tomcat应用,无正常接口调用)
重新访问localhost:8666,效果如下

几乎一半请求出现问题,无法正常显示
这时候给nginx负载增加weight

upstream mytest{
  server www.xff.xyz:8699 weight=5;
  server www.xff.xyz:8080;
 }

显示如下,少量请求未正常显示

再修改负载方式为ip_hash

upstream mytest{
  ip_hash;
  server www.xff.xyz:8699;
  server www.xff.xyz:8080;
 }

可以看到显示完全正常

ip_hash可以保存一次请求所有来源都来自同一服务器,这种负载方式支持session的保持
如有服务器不再使用,需要在该服务器后面添加down关键字,此外还有多种其他关键字参数可设置。


nginx其他多种用法

https://blog.csdn.net/weixin_42767604/article/details/105322113
https://blog.csdn.net/qq_14940627/article/details/80726831


← 【postman】使用postman生成快速批量请求 【redis】记一次性能排查记录 →


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK