您的位置:首页 > 运维架构 > Nginx

linux 部署多个.net core项目,使用nginx分配访问地址

2020-01-15 10:26 323 查看

1. 环境搭建
linux cenos 7以上,.net core 2.2作为测试环境,部署两个服务作为例子
1)下载运行时压缩包(微软官网下载) aspnetcore-runtime-2.2.8-linux-x64.tar.gz
2)压缩包上传到liunx用户指定路径
3)进入压缩包所在目录 运行 tar -zxvf aspnetcore-runtime-2.2.8-linux-x64.tar.gz
4)解压后的文件我放到了/usr/dotnet中
2. 项目发布与上传
1)将发布后的项目分别放到用户指定目录
注意:单个项目默认端口是5000,如果想要多个项目就要修改端口号,要在第二个项目的Program.cs文件中 WebHost.CreateDefaultBuilder(args)后添加调用UseUrls(“http://*:5001”),表示第二个项目占用5001端口
2)发布成功后将两个项目分别放到/home/test/www/api/和/home/test/www/app/中
4. 测试服务
使用运行时压缩包解压后目录里面的dotnet开启服务
运行 /usr/dotnet/dotnet /home/test/www/api/api.dll
出现如下面片段表示5000端口的服务开启成功,输入ctrl + c关闭该服务,继续用该方法测试5001的服务

5. 持久化服务
1)进入/etc/syetemd/system
2)创建文件,名字随便起,我起名为testapi,在里面添加此内容

[Service]
WorkingDirectory=/home/test/www/api
ExecStart=/usr/dotnet/dotnet /home/test/www/api/api.dll
Restart=always
RestartSec=10 # Restart service after 10 seconds if dotnet service crashes
SyslogIdentifier=dotnet-example
User=root
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target

第二个文件testapp内容:

[Service]
WorkingDirectory=/home/test/www/app
ExecStart=/usr/dotnet/dotnet /home/test/www/app/app.dll
Restart=always
RestartSec=10 # Restart service after 10 seconds if dotnet service crashes
SyslogIdentifier=dotnet-example
User=root
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target

3)注册服务
sudo systemctl enable ***.service
我分别运行sudo systemctl enable testapi.service与sudo systemctl enable testapp.service 注册服务
4)启动服务
sudo systemctl start ***.service
5)其他命令
检查服务状态:sudo systemctl status ***.service
重启服务:sudo systemctl restart ***.service
关闭服务::sudo systemctl stop ***.service
6. 安装nginx
1)下载nginx安装包并解压
我这里是下载的openresty的压缩包,openresty是nginx的升华版,基本用法跟 nginx一样我解压到/usr/local/opentesty/中
2)配置环境变量
进入/etc/profile中,添加export PATH=$PATH:/usr/local/openresty/nginx/sbin/
3)测试配置
nginx -V
7. nginx分配访问接口
1)打开/usr/local/openresty/nginx/conf/nginx.conf
在server{}中添加

location /app {  //app是接口前缀例如 http://192.168.0.1/app/test
proxy_pass http://localhost:5001/app;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /api {  //api是接口前缀例如 http://192.168.0.1/api/test
proxy_pass http://localhost:5000/api;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}

2)保存后我分别访问http://192.168.0.1/app/test和http://192.168.0.1/api/test就能分别指向两个服务了

  • 点赞
  • 收藏
  • 分享
  • 文章举报
a250029634 发布了2 篇原创文章 · 获赞 0 · 访问量 128 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐