在這里我就不介紹如何在Linux上部署.Net Core以及進(jìn)程守護(hù)監(jiān)控等內(nèi)容,如果需要可以查看之前發(fā)布的文章。
ASP.NET Core內(nèi)置了Kestrel服務(wù)器,但功能簡單,主要用于SelfHost,正式運(yùn)行還是要依賴IIS、Apache、Nginx等功能全面的服務(wù)器,為ASP.NET Core程序提供類似緩存、壓縮請求、SSL終端等高深的特性或功能。這兩種服務(wù)器的關(guān)系是:Nginx、IIS等作為Kestrel的反向代理服務(wù)器。
Nginx簡介
- Nginx是一個(gè)免費(fèi)的,開源的,高性能的HTTP服務(wù)器和反向代理,以及IMAP / POP3代理服務(wù)器。
- Nginx以其高性能,穩(wěn)定性,豐富的功能集,簡單的配置和低資源消耗而聞名。
- Nginx使用更加可擴(kuò)展的事件驅(qū)動(dòng)(異步)架構(gòu),此體系結(jié)構(gòu)在負(fù)載下使用較小但更重要的可預(yù)測內(nèi)存量。即使您不希望同時(shí)處理數(shù)千個(gè)請求,您仍然可以從Nginx的高性能和小內(nèi)存占用中受益。
- Nginx可以向各個(gè)方向擴(kuò)展:從最小的VPS一直到大型服務(wù)器集群。
Nginx中文文檔:https://www.nginx.cn/doc/
1.安裝Nginx
#安裝epel
sudo yum install epel-release
#安裝Nginx
sudo yum install nginx
#啟動(dòng)Nginx,它不會自己啟動(dòng)
sudo systemctl start nginx

2.開放80端口
#開放端口
sudo firewall-cmd --zone=public --add-port=80/tcp --permanent
#重啟防火墻
sudo firewall-cmd --reload
3.訪問驗(yàn)證

4.Nginx開機(jī)自啟動(dòng)
#Nginx默認(rèn)是不主動(dòng)開啟的,為了能夠在系統(tǒng)啟動(dòng)就開啟Nginx
sudo systemctl enable nginx
5.端口映射配置
#編輯nginx.conf
sudo vim /etc/nginx/nginx.conf
#進(jìn)入文件后,按“i”或者“a”進(jìn)入插入模式,插入下面的配置信息
進(jìn)去注釋掉http配置下server的默認(rèn)配置內(nèi)容
# 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! (保存編輯強(qiáng)制退出)
:w ! sudo tee %
注釋完原來的映射之后,我們需要在/etc/nginx/conf.d文件夾下為.Net Core項(xiàng)目新建一個(gè)DemoNetCore.conf文件,文件配置如下
#進(jìn)入conf.d文件夾
cd /etc/nginx/conf.d
#創(chuàng)建配置文件
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,重啟的時(shí)候可能會直接報(bào)錯(cuò),如下圖
#重啟Nginx
sudo systemctl restart nginx

這個(gè)時(shí)候我們先找到Nginx相關(guān)進(jìn)程,然后直接干掉,然后再啟動(dòng)
#查看Nginx進(jìn)程
ps aux | grep nginx
#殺死相關(guān)進(jìn)程
sudo kill -9 2058
sudo kill -9 2059
#重啟Nginx
sudo systemctl restart nginx

6.驗(yàn)證效果

這里顯示502 Bad Gateway,原因是SELinux配置的問題
安全增強(qiáng)型 Linux(Security-Enhanced Linux)簡稱 SELinux,它是一個(gè) Linux 內(nèi)核模塊,也是 Linux 的一個(gè)安全子系統(tǒng)。
SELinux 主要由美國國家安全局開發(fā)。2.6 及以上版本的 Linux 內(nèi)核都已經(jīng)集成了 SELinux 模塊。
SELinux 的結(jié)構(gòu)及配置非常復(fù)雜,而且有大量概念性的東西,要學(xué)精難度較大。很多 Linux 系統(tǒng)管理員嫌麻煩都把 SELinux 關(guān)閉了。
如果可以熟練掌握 SELinux 并正確運(yùn)用,我覺得整個(gè)系統(tǒng)基本上可以到達(dá)"堅(jiān)不可摧"的地步了(請永遠(yuǎn)記住沒有絕對的安全)。
掌握 SELinux 的基本概念以及簡單的配置方法是每個(gè) Linux 系統(tǒng)管理員的必修課。
所以出現(xiàn)這個(gè)問題有兩種解決方案:
①、直接關(guān)閉SELinux
#進(jìn)入SELinux目錄
cd /etc/selinux
#編輯selinux config配置文件
sudo vim config
#修改配置:SELINUX=disabled,保存退出
保存好之后,從enforcing或permissive改為diabled,需要重啟系統(tǒng)之后才能生效。當(dāng)我們重啟之后,可以看到下圖訪問正常了。

②、將Nginx添加至SELinux的白名單
yum install policycoreutils-python
cat /var/log/audit/audit.log | grep nginx | grep denied | audit2allow -M mynginx
semodule -i mynginx.pp
