把Asp.Net Core 2.0部署在Linux上,使用Nginx代理服務器,而且用Systemctl命令以服務的方式監聽項目

在Linux上部署.net core 2.0程序:nginx

第一步:配置Nginx代理api

在/etc/nginx/sites-available/default 中添加   服務器

server {
        listen 80;
        location /{
        proxy_pass http://localhost:5000;
        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;
        }
}

這樣,訪問80端口,就能夠直接訪問到localhost:5000端口了mvc

第二步:若是一臺服務器裏要運行多個站點,就要配置Nginx 按照域名轉發app

server {
        listen 80;
        server_name test1.api.com;
        location /{
        proxy_pass http://localhost:5000;
        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;
        }
}

server {
        listen 80;
        server_name test2.api.com;
        location /{
        proxy_pass http://localhost:5001;
        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;
        }
}

這樣的話,經過域名 test1.api.com:80 訪問就是localhost:5000的站點,經過域名test2.api.com:80 訪問的就是localhost:5001的站點工具

/etc/nginx/nginx.conf 中的http{}中加入:測試

 include /etc/nginx/sites-enabled/*;

測試新增的配置是否正確spa

nginx -t

從新加載配置.net

nginx -s reload

第三步:設置守護進程代理

設置守護進程有不少方法,這裏介紹利用Linux中的系統服務管理工具 Systemctl 。也是很方便的。

在/etc/systemd/system/ 文件夾下,新建一個test.service

[Unit]
Description = CNBlogs.ZzkService running on Ubuntu

[Service]
WorkingDirectory = /test
ExecStart =/usr/bin/dotnet /test/bin/Debug/netcoreapp2.0/CNBlogs.ZzkService.WebApi.dll
Restart = always
RestartSec = 3
SyslogIdentifier = dotnet-example
User = root
Environment = ASPNETCORE_ENVIRONMENT=Production

Environment = DOTNET_PRINT_TELEMETRY_MESSAGE=false

ExecStart 是運行命令

RestartSec 是每3秒檢查一次

啓動服務

systemctl enable test.service
systemctl start test.service

查看服務運行狀態

systemctl status test.service

會出現相似下面的狀態,表示運行正確:

● kestrel-hellomvc.service - Example .NET Web API Application running on Ubuntu
    Loaded: loaded (/etc/systemd/system/kestrel-hellomvc.service; enabled)
    Active: active (running) since Thu 2016-10-18 04:09:35 NZDT; 35s ago
Main PID: 9021 (dotnet)
    CGroup: /system.slice/kestrel-hellomvc.service
            └─9021 /usr/local/bin/dotnet /var/aspnetcore/hellomvc/hellomvc.dll
相關文章
相關標籤/搜索