把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
相关文章
相关标签/搜索