美文网首页
使用Nginx 反向代理asp.net core程序

使用Nginx 反向代理asp.net core程序

作者: 高呼吾名 | 来源:发表于2020-06-08 10:41 被阅读0次

发布并运行asp.net core程序。

asp.net core 程序发布后默认点端口有俩个http://localhost:5000https://localhost:5001 ,只能通过locaohost访问。 使用Nginx 反向代理用这俩个地址和端口即可。

如需要在同台服务器部署两个程序,端口冲突时,可在Program.cs中点CreateHostBuilder()中 使用 UseUrls()修改。

  public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder => {
                    webBuilder.UseStartup<Startup>();
                    webBuilder.UseUrls("http://*:5555", "https://*:5666");
                    //这里填写*号即为可以匹配所有指向本主机的域名。
                    //以此种方式配置后,在发布运行时,当安全策略端口被允许时 即可被外网访问
                    //asp.net core 默认设置发布后,不可被外网访问是因为他默认配置指定里localhost域名。
                });

在使用Nginx 反向代理的情形下 这里配置的端口无须对外开放。

当asp.net core 程序成功运行时,打开Nginx 的配置文件nginx.conf,找到http代码块,复制一个默认的server代码块(nginx 配置文件中有默认被注释掉的)一个http代码块中可以存在多个server代码块。

    server {
        listen        7999; #监听的端口 也就是网站程序将来要使用点端口
        server_name  www.xxxx.com; #域名,如无域名可以不写,访问时直接用ip地址即可
        location / {
          #跨域设置
          add_header Access-Control-Allow-Origin *;
          add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
          add_header Access-Control-Allow-Headers 'DNT,token,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';

         if ($request_method = 'OPTIONS') {
              return 204;
          }
          proxy_http_version 1.1;
          proxy_pass http://127.0.0.1:9526; #反向代理;
        }
    }

重启nginx 完成。

相关文章

网友评论

      本文标题:使用Nginx 反向代理asp.net core程序

      本文链接:https://www.haomeiwen.com/subject/fxzazhtx.html