1、访问"https://nginx.org/en/download.html"下载Nginx。
2、在本地解压Nginx压缩包,找到"conf"->“nginx.conf”,右键打开nginx.conf,server中的listen表示nginx监听的端口,默认为80,将默认端口号改为8081(#表示注释,不使用80是因为本地有其他项目使用该端口)
3、其中下图圈起来的是nginx执行程序,双击运行即可(会看到命令行一闪而过)
4、浏览器中访问"http://localhost:8081",可看到下图所示的界面,该界面放在html文件夹中(注:现在vue等单体应用,使用nginx时都是将build生成的dist文件夹中的文件复制到html文件中,就可直接访问)
5、nginx常用命令:
2、查看当前nginx版本:nginx -v
3、启动nginx:start nginx
4、关闭nginx(退出前完成已经接受的连接请求):nginx -s quit
5、关闭nginx(快速关闭,不管有没有正在处理的请求):nginx -s stop
6、重启nginx:nginx -s reload
1、打开VS2019,创建一个名为"NginxTest"的web api项目,如下图:
2、将WeatherForecast控制器中的Get()方法内容 做如下更改,其中使用到的IConfiguration可用来获取执行命令时指定的参数或appsetting.json中的参数,后面通过命令行部署net core应用时会传递参数,以做区分。
using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace NginxTest.Controllers { [ApiController] [Route("[controller]")] public class WeatherForecastController : ControllerBase { private static readonly string[] Summaries = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" }; private readonly ILogger<WeatherForecastController> _logger; private IConfiguration _configuration; public WeatherForecastController(ILogger<WeatherForecastController> logger, IConfiguration configuration) { _logger = logger; _configuration = configuration; } [HttpGet] public string Get() { //获取启动时的IP和Port(端口) string json = "NginxTest:" + _configuration["ip"] + ":" + _configuration["port"]; return json; } } }
3、右键重新生成"NginxTest"项目,重新生成成功后,右键"在文件资源管理器中打开文件夹(X)“来到项目所在目录,进入"bin/debug"文件夹,可看到生成的"NginxTest.dll”,在路径中输入"cmd"(即CMD进入该路径)
4、在命令行中输入如下命令,其中urls表示服务部署在666端口,–port和ip表示传递的参数,可通过上面讲到的IConfiguration获取到(注:–port和ip可写随意写,这是我们传递的参数,这样写是后面我们开多个微服务时起一定的标识作用)
dotnet NginxTest.dll --urls="http://*:666" --port=666 --ip="127.0.0.1" dotnet NginxTest.dll --urls="http://*:777" --port=777 --ip="127.0.0.1"
1、打开nginx中的"conf"文件夹中的nginx.conf文件,做如下更改:
在server上增加upstream webapi{}块,webapi是自定义的名称,其中存放的server是我们
upstream webApi { server 127.0.0.1:666; server 127.0.0.1:777; }
将server中的监听端口更改为8081,location中配置上面定义的服务器列表
location / { proxy_pass http://webApi; #请求转向webApi 定义的服务器列表 }
重新启动nginx 在nginx.exe所在路径进入cmd执行如下指令即可
nginx -s reload
2、浏览器中访问"http://localhost:8081/weatherforecast",并刷新几次,可看到输出内容的端口在666和777之间转换(注:nginx默认的策略就是轮询,即服务器列表中的服务会依次循环访问),如下图:
3、打开nginx.conf中文件,更改服务器列表中对应服务的权重,如下更改666端口的权重为2,777端口权重为1,并使用"nginx -s reload"命令重启nginx服务器。
upstream webApi { server 127.0.0.1:666 weight=2; server 127.0.0.1:777 weight=1; }
4、访问8081端口时,每次轮询,666端口是两次,777是1次(可通过不断刷新界面进行观察),如下图:
5、完成的nginx.conf文件如下:
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; 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 on; upstream webApi { server 127.0.0.1:666 weight=2; server 127.0.0.1:777 weight=1; } server { listen 8081; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://webApi; #请求转向webApi 定义的服务器列表 } #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; #} } # 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; # } #} }
- net core+webapi+nginx windows 服务器部署(1321)
- .Nuget Packages 太占C盘,删除后可以放到其他盘(1194)
- ASP.NET Core 配置 Swagger 显示接口注释描述信息(1094)
- vue调用接口后获取不到后端返回的Header响应头(963)
- .net core 系列实例开发教程-权限管理系统功能介绍(951)
- .net core 6.0 web API + SwaggerUI + IIS部署(911)
- .net core 实例教程(十二)配置启用Swagger中的【Authorize】按钮(830)
- .net core 实例教程(一)新建项目(795)
- .net core 实例教程(十四)配置 Swagger 显示接口注释描述信息及支持版本控制(781)
- .net 6中使用log4net写sqlserver数据库日志(755)
- 2025年3月 (1)
- 2024年6月 (2)
- 2024年5月 (2)
- 2024年4月 (4)
- 2024年3月 (30)
- 2024年1月 (4)
- 2023年12月 (2)
- 2023年11月 (4)
- 2023年10月 (4)
- 2023年9月 (6)
- 2023年3月 (2)
- 2023年2月 (1)
- 2023年1月 (1)
- 2022年12月 (1)
- 2022年9月 (21)
- 2022年8月 (10)
- 2022年7月 (3)
- 2022年4月 (1)
- 2022年3月 (13)
- 2021年8月 (1)
- 2021年3月 (1)
- 2020年12月 (42)
- 2020年11月 (7)
- 2020年10月 (5)
- 2020年8月 (1)
- 2020年6月 (1)
- 2020年3月 (2)
- 2019年12月 (8)
- 2019年11月 (3)
- 2019年9月 (1)
- 2019年4月 (1)
- 2019年3月 (6)
- 2019年2月 (1)
- 2018年7月 (7)
- 1.asp.net mvc内微信pc端、H5、JsApi支付方式总结(5702)
- 2.各大搜索网站网站收录提交入口地址(3201)
- 3.Windows 10休眠文件更改存储位置(3162)
- 4.ECharts仪表盘实例及参数使用详解(3095)
- 5.windows 10安装myeclipse 10破解补丁cracker.jar、run.bat闪退解决办法(2991)
- 6.HTML5 WebSocket与C#建立Socket连接实现代码(2865)
- 7.华为鸿蒙系统清除微信浏览器缓存方法(2777)
- 8.CERT_HAS_EXPIRED错误如何解决(2239)
- 9.Js异步async、await关键字详细介绍(lambda表达式中使用async和await关键字)(2187)
- 10.HBuilder编辑器格式化代码(2118)