nssm工具的作用:把項(xiàng)目部署成Windows服務(wù),可以在系統(tǒng)后臺(tái)運(yùn)行
1.創(chuàng)建一個(gè)asp.net core mvc的項(xiàng)目weblication1
asp.net core mvc項(xiàng)目要成為windows服務(wù)需要安裝下面的nuget包
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="8.0.0" />
</ItemGroup>
在program中添加代碼
在創(chuàng)建一個(gè)asp.net core mvc項(xiàng)目 webapplication2
把program中的usePathBase改為(“/app2”)
我這里沒添加
builder.Host.UseWindowsService();也成功了。有可能是我使用.net 8版本的原因,低版本的應(yīng)該不可以。
2.發(fā)布項(xiàng)目(兩個(gè)項(xiàng)目都發(fā)布)
3.使用nssm工具安裝服務(wù)
這里就不提供安裝包了,可以自己在網(wǎng)上下載
找到文件目錄,在地址欄輸入 cmd
安裝 命令:nssm install
進(jìn)入圖形化界面
配置項(xiàng)說明:
Path:運(yùn)行應(yīng)用程序的程序
Startup directory:應(yīng)用程序所在的目錄
Arguments:應(yīng)用運(yùn)行的參數(shù)
Service name:生成服務(wù)的名稱
最后點(diǎn)擊install service 完成windows服務(wù)安裝,在windows服務(wù)列表就能看到創(chuàng)建的服務(wù)了。
常用命令:
nssm install servername //創(chuàng)建servername服務(wù),彈出配置界面
nssm start servername //啟動(dòng)服務(wù)
nssm stop servername //暫停服務(wù)
nssm restart servername //重新啟動(dòng)服務(wù)
nssm remove servername //刪除創(chuàng)建的servername服務(wù)
nssm edit servername//更改servername服務(wù),彈出修改界面
nssm set servername 參數(shù)名 參數(shù)值 //設(shè)置服務(wù)參數(shù)值
sc delete servername//windows刪除服務(wù)命令
直接使用windows的服務(wù)管理也可以實(shí)現(xiàn)服務(wù)的操作,服務(wù)右鍵屬性 - 恢復(fù)即可設(shè)置服務(wù)掛掉重啟等內(nèi)容。
將應(yīng)用作成服務(wù)(兩個(gè)項(xiàng)目都要,記得改名稱)
1.應(yīng)用的啟動(dòng)命令是:
dotnet WebApplication1.dll --urls=http://*:8888/ --port=8888
2、安裝服務(wù):
安裝兩個(gè)一個(gè)8888,一個(gè)8889。這邊就演示一遍
Path:C:\Program Files\dotnet\dotnet.exe
Startup directory:C:\Users\pzx\source\repos\WebApplication1\WebApplication1\bin\Release\net8.0\publish
Arguments:WebApplication1.dll --urls=http://localhost:8888/ --port=8888
Service name:webapp1
填入上面的信息,提示succful 安裝成功!
查看服務(wù),是否已安裝。
需要,右擊啟動(dòng)服務(wù)(服務(wù)可以設(shè)置為自動(dòng),服務(wù)器重啟了,也可以直接訪問)
webapp1
直接在瀏覽器里輸入:http://localhost:8888/
webapp2
下載nginx服務(wù)器
下載地址
https://nginx.org/en/download.html
選擇穩(wěn)定版,windows版本
雙擊exe即可,這個(gè)是綠色版的,無需安裝
安裝成功(默認(rèn)80端口)輸入IP或localhost 顯現(xiàn)下面頁面
配置ngixn.conf
在server里面添加下面的配置:
location / {
root html;
index index.html index.htm;
}
location /app1 {
# 去除路徑前綴,以便正確代理
rewrite ^/app1(/.*)$ $1 break;
proxy_pass http://localhost:8888;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 300s;
proxy_redirect off;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_next_upstream http_502 http_504 error timeout invalid_header;
}
#此配置css,js失效時(shí)可添加
location ~ .*\.(js|css|jpg|jpeg|gif|png|ico|pdf|txt)$ {
# 去除路徑前綴,以便正確代理
rewrite ^/app1(/.*)$ $1 break;
proxy_pass http://localhost:8888;
}
location /app2 {
# 去除路徑前綴,以便正確代理
rewrite ^/app2(/.*)$ $1 break;
proxy_pass http://localhost:8889;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 300s;
proxy_redirect off;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_next_upstream http_502 http_504 error timeout invalid_header;
}
#此配置css,js失效時(shí)可添加
location ~ .*\.(js|css|jpg|jpeg|gif|png|ico|pdf|txt)$ {
# 去除路徑前綴,以便正確代理
rewrite ^/app2(/.*)$ $1 break;
proxy_pass http://localhost:8889;
}
修改后,要把nginx進(jìn)程結(jié)束,在重啟啟動(dòng)
直接在瀏覽器輸入
http://localhost/app1
http://localhsot/app2
上面就一臺(tái)服務(wù)器代理兩個(gè)不同的服務(wù)。
Linux下也一樣的配置
使用 vim 打開
vim /etc/nginx/sites-enabled/default
配置一樣
nginx語法檢查文章來源:http://www.zghlxwxcb.cn/news/detail-845015.html
nginx -t
重啟服務(wù)文章來源地址http://www.zghlxwxcb.cn/news/detail-845015.html
systemctl restart nginx
到了這里,關(guān)于nssm 工具把a(bǔ)sp.net core mvc變成 windows服務(wù),使用nginx反向代理訪問的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!