ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

Linux(Centos 7)使用Nginx代理.NET Core 项目

2020-04-05 13:03:52  阅读:173  来源: 互联网

标签:Core http Centos Nginx sudo server nginx conf Linux


  在这里我就不介绍如何在Linux上部署.Net Core以及进程守护监控等内容,如果需要可以查看之前发布的文章。

  ASP.NET Core内置了Kestrel服务器,但功能简单,主要用于SelfHost,正式运行还是要依赖IIS、Apache、Nginx等功能全面的服务器,为ASP.NET Core程序提供类似缓存、压缩请求、SSL终端等高深的特性或功能。这两种服务器的关系是:Nginx、IIS等作为Kestrel的反向代理服务器。

Nginx简介

  • Nginx是一个免费的,开源的,高性能的HTTP服务器和反向代理,以及IMAP / POP3代理服务器。
  • Nginx以其高性能,稳定性,丰富的功能集,简单的配置和低资源消耗而闻名。
  • Nginx使用更加可扩展的事件驱动(异步)架构,此体系结构在负载下使用较小但更重要的可预测内存量。即使您不希望同时处理数千个请求,您仍然可以从Nginx的高性能和小内存占用中受益。
  • Nginx可以向各个方向扩展:从最小的VPS一直到大型服务器集群。

Nginx中文文档:https://www.nginx.cn/doc/

 

1.安装Nginx

 

#安装epel
sudo yum install epel-release

#安装Nginx
sudo yum install nginx

#启动Nginx,它不会自己启动
sudo systemctl start nginx

 

 

2.开放80端口

 

#开放端口
sudo firewall-cmd --zone=public --add-port=80/tcp --permanent

#重启防火墙
sudo firewall-cmd --reload

 

3.访问验证

 

 

4.Nginx开机自启动

 

#Nginx默认是不主动开启的,为了能够在系统启动就开启Nginx
sudo systemctl enable nginx

 

5.端口映射配置

 

#编辑nginx.conf
sudo vim  /etc/nginx/nginx.conf

#进入文件后,按“i”或者“a”进入插入模式,插入下面的配置信息

 

进去注释掉http配置下server的默认配置内容

 

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

#    server {
#        listen       80 default_server;
#        listen       [::]:80 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

 

#按ESC,输入命令保存配置文件
:wq   (保存编辑操作退出)
:wq!  (保存编辑强制退出)
:w ! sudo tee %

 

注释完原来的映射之后,我们需要在/etc/nginx/conf.d文件夹下为.Net Core项目新建一个DemoNetCore.conf文件,文件配置如下

 

#进入conf.d文件夹
cd /etc/nginx/conf.d

#创建配置文件
sudo touch DemoNetCore.conf

#编辑配置文件
sudo vim DemoNetCore.conf

#配置文件信息
server {
    listen       80;
    location / {
    proxy_pass http://localhost:8081;
    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;
    }
}

 

配置好之后重启Nginx,重启的时候可能会直接报错,如下图

 

#重启Nginx
sudo systemctl restart nginx

 

  

这个时候我们先找到Nginx相关进程,然后直接干掉,然后再启动

 

#查看Nginx进程
ps aux | grep nginx

#杀死相关进程
sudo kill -9 2058
sudo kill -9 2059

#重启Nginx
sudo systemctl restart nginx

 

 

6.验证效果

 

  

这里显示502 Bad Gateway,原因是SELinux配置的问题

 

 安全增强型 Linux(Security-Enhanced Linux)简称 SELinux,它是一个 Linux 内核模块,也是 Linux 的一个安全子系统。
SELinux 主要由美国国家安全局开发。2.6 及以上版本的 Linux 内核都已经集成了 SELinux 模块。
SELinux 的结构及配置非常复杂,而且有大量概念性的东西,要学精难度较大。很多 Linux 系统管理员嫌麻烦都把 SELinux 关闭了。
如果可以熟练掌握 SELinux 并正确运用,我觉得整个系统基本上可以到达"坚不可摧"的地步了(请永远记住没有绝对的安全)。
掌握 SELinux 的基本概念以及简单的配置方法是每个 Linux 系统管理员的必修课。

 

所以出现这个问题有两种解决方案:

 

①、直接关闭SELinux

 

#进入SELinux目录
cd /etc/selinux

#编辑selinux config配置文件
sudo vim config

#修改配置:SELINUX=disabled,保存退出

 

保存好之后,从enforcing或permissive改为diabled,需要重启系统之后才能生效。当我们重启之后,可以看到下图访问正常了。

 

  

②、将Nginx添加至SELinux的白名单

 

yum install policycoreutils-python  
cat /var/log/audit/audit.log | grep nginx | grep denied | audit2allow -M mynginx  
semodule -i mynginx.pp 

 

 

标签:Core,http,Centos,Nginx,sudo,server,nginx,conf,Linux
来源: https://www.cnblogs.com/jayjiang/p/12636767.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有