pc做网站,国外metro风格网站模板,企业门户网站设计建设与维护,莱州人才招聘网一、下载和启动
1.下载、使用命令行启动#xff1a;Web开发#xff1a;web服务器-Nginx的基础介绍#xff08;含AI文稿#xff09;_nginx作为web服务器,可以承担哪些基本任务-CSDN博客
注意#xff1a;我配置的端口是81 2.测试连接是否正常
访问Welcome to nginx! 如果…一、下载和启动
1.下载、使用命令行启动Web开发web服务器-Nginx的基础介绍含AI文稿_nginx作为web服务器,可以承担哪些基本任务-CSDN博客
注意我配置的端口是81 2.测试连接是否正常
访问Welcome to nginx! 如果没有报错出现下面的界面则表示Nginx配置正常 二、常用命令
1.启动命令
start nginx
2.退出命令
选择1安全退出
nginx -s quit
选择2强制退出stop 参数会立即停止 Nginx 进程而 quit 参数会等待当前请求处理完成后再停止
nginx -s stop
选择3强制杀掉线程查询进程号和终止其所有进程管理员身份运行cmd
tasklist | findstr nginx.exetaskkill /f /t /im nginx.exe 3.重新加载配置文件使新的配置生效
nginx -s reload 三、常见玩法
1.映射访问静态资源
第一步建立文件
E:\Test\nginx-logo.png 第二步Nginx配置映射
listen 81;
server_name localhost;location /PicTest
{alias E:/Test;
} 说直白点就是访问/PicTest相当于访问E:/Test 保存了conf配置文件之后需要去执行一下reload命令才保存成功
第三步用浏览器访问以下两个url都可以正常访问到图片说明映射成功
E:/Test/nginx-logo.pnghttp://localhost:81/PicTest/nginx-logo.png
附上完整配置文件(nginx.conf)
worker_processes 1;events {worker_connections 1024;
}http {include mime.types;default_type application/octet-stream;sendfile on;keepalive_timeout 65;server {listen 81;server_name localhost;location /PicTest{alias E:/Test;}location / {root html;index index.html index.htm;}error_page 500 502 503 504 /50x.html;location /50x.html {root html;}}
}2.负载均衡转发接口
第一步确保下面两个接口能正常收到数据
http://localhost:5050/api/getDatahttp://localhost:5055/api/getData
第二步配置Nginx
效果是当访问 http://localhost:81/api/getData 时都有概率打到上面的两个接口中权重5055:50502:1
http {upstream backend {# 定义负载均衡的后端服务器并设置权重server localhost:5050 weight1; # 5050 端口的服务器权重 1server localhost:5055 weight2; # 5055 端口的服务器权重 2}server {listen 81; # 监听 81 端口location /api/getData {# 转发请求到 upstream 定义的服务器集群proxy_pass http://backend; # 使用上面定义的 upstream 名称proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}}
}保存了conf配置文件之后需要去执行一下reload命令才保存成功
第三步访问Nginx端口
http://localhost:81/api/getData
访问上面接口时能正常返回数据并且发现每请求4次5055后会请求2次5050权重21并且循环如此说明均衡负载配置成功
附上完整配置文件(nginx.conf)
worker_processes 1;events {worker_connections 1024;
}http {include mime.types;default_type application/octet-stream;sendfile on;keepalive_timeout 65;upstream backend {# 定义负载均衡的后端服务器并设置权重server localhost:5050 weight1; # 5050 端口的服务器权重 1server localhost:5055 weight2; # 5055 端口的服务器权重 2}server {listen 81;server_name localhost;location /api/getData {# 转发请求到 upstream 定义的服务器集群proxy_pass http://backend; # 使用上面定义的 upstream 名称proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}location / {root html;index index.html index.htm;}error_page 500 502 503 504 /50x.html;location /50x.html {root html;}}
}