NetCore在Linux上部署

工具:WMWare虚拟机,Wmware12,CentOS7ISO镜像,VS2017

1、安装虚拟机,过程略,网上一搜一大把

2、用VS2017建一个NetCore的Web项目,用命令行生成也可以,然后发布

3、搭建Linux下的NetCore运行环境

  Linux下访问这个网站:https://www.microsoft.com/net/download/windows

  

  点击红色的部分,然后跳转到另一个页面,同时下载文件

  

  按照上面的步骤配置好NetCore SDK。(个人认为使用Binaries方式简单些,如果你想换另一种方式,我没有异议)

4、windows下的发布包直接复制到主文件夹里面(home目录),然后进入复制过来的发布目录下,直接dotnet 项目名称.dll就可以访问5000端口了。

5、如果出现错误,请自己排查,大部分都是NetCore版本问题,也就是你VS生成的项目的NetCore版本和Linux上NetCore的版本不匹配

Nginx集群部署

1、这就需要在Home/Index文件上动手脚了,下面是我的代码

 using Cluster.Models;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics; namespace Cluster.Controllers
{
public class HomeController : Controller
{
public IActionResult Index()
{
ViewBag.RemoteIpAddress = Request.HttpContext.Connection.RemoteIpAddress;
ViewBag.Header = Request.Headers["X-Forwarded-For"];
ViewBag.LocalIpAddress = HttpContext.GetClientUserIp();
ViewBag.RequestHeaders = Request.Headers;
return View();
} public IActionResult About()
{
ViewData["Message"] = "Your application description page."; return View();
} public IActionResult Contact()
{
ViewData["Message"] = "Your contact page."; return View();
} public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}
 using Microsoft.AspNetCore.Http;
using System.Linq; namespace Cluster.Models
{
public static class IPExtension
{
public static string GetClientUserIp(this HttpContext context)
{
var ip = context.Request.Headers["X-Forwarded-For"].FirstOrDefault();
if (string.IsNullOrEmpty(ip))
{
ip = context.Connection.LocalIpAddress.ToString();
} return ip;
}
}
}
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; namespace Cluster
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
} public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
} // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
//添加转发设置
app.UseForwardedHeaders(new ForwardedHeadersOptions {
ForwardedHeaders = Microsoft.AspNetCore.HttpOverrides.ForwardedHeaders.XForwardedFor | Microsoft.AspNetCore.HttpOverrides.ForwardedHeaders.XForwardedProto
});
app.UseStaticFiles(); app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}
 @{
ViewData["Title"] = "Home Page";
} 客户端:<br />
Request.HttpContext获取访问ip: @ViewBag.RemoteIpAddress
<br />
Request.Headers获取访问ip:@ViewBag.Header
<br />
服务端:<br />
Request.HttpContext获取响应服务所在服务器的ip:@ViewBag.LocalIpAddress
<table class="table">
<thead>
<tr>
<th>key</th>
<th>value</th>
</tr>
</thead>
<tbody>
@foreach (var item in ViewBag.RequestHeaders)
{
<tr>
<td>@item.Key</td>
<td>@item.Value</td>
</tr>
}
</tbody>
</table>

我的是:Windows下把发布包挂到了IIS上,然后有个访问地址,Linux下直接dotnet命令启动,还是本地访问,所以上面的代码,对我来说没啥用

2、Linux安装Nginx,Nginx作为反向代理服务器会把接受的请求转发给对应的Server,不过是随机的,Nginx安装过程略

3、修改Nginx配置文件

 #集群站点配置
upstream xxx.services{
server IP地址1:端口1 fail_timeout=60s;
server IP地址2:端口2 fail_timeout=60s;
} server {
#代理监听端口
listen default_server;
listen [::]: default_server; root /var/www/html; server_name _; #_默认ip+端口访问,_可以替换成访问域名如:shenniu.core.com
#缓存文件路由
location ~ .*(\.(js|css|jpg|svg)).* { proxy_pass http://shenniu.services;
proxy_cache_valid ;
proxy_cache my_cache;
expires 3d;
}
#集群站点路由
location / { proxy_pass http://xxx.services;
#对应upstream后面的名称
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_cache_bypass $http_upgrade; proxy_set_header X-Forwarded-For
$proxy_add_x_forwarded_for;
}
}

注意用Root用户修改,有的Nginx.Conf文件是只读的,修改完之后 Nginx -r reload一下

然后访问Nginx吧,不出意外的话你就可以看到效果了

备注:部署集群是为了提高性能的,Nginx作为一个反向代理服务器在集群部署方面还是很不错的

NetCore在Centos7上部署和Nginx集群部署访问的更多相关文章

  1. Nginx 集群部署(Keepalived)

    # Nginx集群部署 # 当我们的用户同时访问量达到一定量的时候,一台服务器是不够用的 # 这个时候我们需要解决这个问题肯定是要添加新的服务器去处理用户访问 # 多台服务器处理用户访问就需要我们集群 ...

  2. Linux Centos7.5中的RocketMQ集群部署

    系统环境 Docker > centos7.5 此镜像已经安装了jdk1.8和maven3.6.0 如果你想知道这个基础镜像的具体情况, 参考此文: https://www.cnblogs.co ...

  3. .netcore consul实现服务注册与发现-集群部署

    一.Consul的集群介绍 Consul Agent有两种运行模式:Server和Client.这里的Server和Client只是Consul集群层面的区分,与搭建在Cluster之上的应用服务无关 ...

  4. Windows上搭个Nginx集群环境玩玩

    一.在windows上安装nginx 1.从这里下载nginx的windows版本 2.把压缩文件解压至c盘根目录,并将文件夹重命名成nginx 3.在conf目录下的nginx.conf文件中,指定 ...

  5. Kubernetes集群部署之三ETCD集群部署

    kuberntes 系统使用 etcd 存储所有数据,本文档介绍部署一个三节点高可用 etcd 集群的步骤,这三个节点复用 kubernetes 集群机器k8s-master.k8s-node-1.k ...

  6. docker 部署 HFish(集群部署)

    主节点部署: docker run -d --name hfish-master -p : -p : -p : -p : -p : -p : -p : -p : -p : -p : -p : -p : ...

  7. Linux之FineBI集群部署

    在企业应用中,通常单个计算机的配置是有限的,而企业应用又是高并发的需求,这个时候会通过计算机集群的方式来提高并发数,从而提高整体应用服务的性能.集群是将多台计算机作为一个整体来提供相关应用的服务.Fi ...

  8. kubernetes kubeadm部署高可用集群

    k8s kubeadm部署高可用集群 kubeadm是官方推出的部署工具,旨在降低kubernetes使用门槛与提高集群部署的便捷性. 同时越来越多的官方文档,围绕kubernetes容器化部署为环境 ...

  9. 分布式监控工具Ganglia 介绍 与 集群部署.

    如果你目的很明确就是冲着标题来的,不爱看我唠叨,请直接进入第二个分割线之后的内容. 其实之前就是有做Swift监控平台的打算的,但是因为没什么硬性需求么,也不要紧的,就一直搁置了.最近实验室来了个大二 ...

随机推荐

  1. OD~~helloworld

    要爆破的C程序源码: #include <stdio.h> int main() { int x; scanf("%d",&x); ) printf(" ...

  2. Angular测试遇到的小坑

    Angular测试遇到的小坑 Error: Expected to be running in 'ProxyZone', but it was not found 检查doneFn的写法是否正确,位置 ...

  3. 【BZOJ】1040: [ZJOI2008]骑士 环套树DP

    [题意]给定n个人的ai和bi,表示第i个人能力值为ai且不能和bi同时选择,求能力值和最大的选择方案.n<=10^6. [算法]环套树DP(基环树) [题解]n个点n条边——基环森林(若干环套 ...

  4. 【CodeForces】708 B. Recover the String 数学构造

    [题目]B. Recover the String [题意]找到一个串s,满足其中子序列{0,0}{0,1}{1,0}{1,1}的数量分别满足给定的数a1~a4,或判断不存在.数字<=10^9, ...

  5. 2017ACM暑期多校联合训练 - Team 4 1003 HDU 6069 Counting Divisors (区间素数筛选+因子数)

    题目链接 Problem Description In mathematics, the function d(n) denotes the number of divisors of positiv ...

  6. GSON转换日期数据为特定的JSON数据

    通过JSON传递数据的时候经常需要传递日期,Java中可以通过GSON将日期转换为特定格式的JSON数据. 1.普通的GSON转换日期 public void query(HttpServletReq ...

  7. 用C#实现对MSSqlServer数据库的增删改查---Server层(WaterLevelSetServer.cs、DeviceSetServer.cs)

    在Server层定义WaterLevelSetServer和WaterLevelRecordServer两个子类,分别继承DeviceSetServer和DeviceRecordServer. usi ...

  8. 64_s1

    SAASound-3.2-17.fc26.i686.rpm 13-Feb-2017 22:13 27650 SAASound-3.2-17.fc26.x86_64.rpm 13-Feb-2017 23 ...

  9. Ubuntu连接多台Ubuntu server的问题

    如果您用的是虚拟机上安装的几个Ubuntu server进行IP配置 要注意以下几点: <1>虚拟机上安装完成Ubuntu server 默认的网络连接方式是NAT ,应该改成桥接网卡 ( ...

  10. 7.Python3标准库--文件系统

    ''' Python的标准库中包含大量工具,可以处理文件系统中的文件,构造和解析文件名,还可以检查文件内容. 处理文件的第一步是要确定处理的文件的名字.Python将文件名表示为简单的字符串,另外还提 ...