Nginx知多少系列之(五)Linux下託管.NET Core專案
目錄
1.前言
2.安裝
3.配置檔案詳解
4.工作原理
5.Linux下託管.NET Core專案
6.Linux下.NET Core專案負載均衡
7.Linux下.NET Core專案Nginx+Keepalived高可用(主從模式)
8.Linux下.NET Core專案Nginx+Keepalived高可用(雙主模式)
9.Linux下.NET Core專案LVS+Keepalived+Nginx高可用叢集
10.構建靜態伺服器
11.日誌分析
12.優化策略
13.總結
在這裡我就不介紹如何在Linux上部署.Net Core以及程序守護監控等內容,如果需要可以檢視之前釋出的文章。《.NET Core專案部署到Linux(Centos7)(六)釋出.NET Core 專案到Linux》、《.NET Core專案部署到Linux(Centos7)(七)啟動和停止.NET Core專案》、《.NET Core專案部署到Linux(Centos7)(八)為.NET Core專案建立Supervisor程序守護監控》
ASP.NET Core內建了Kestrel伺服器,但功能簡單,不是一個全功能的Web伺服器,主要用於SelfHost,它旨在使ASP.NET儘可能快,但其管理安全性和提供靜態檔案的能力有限,正式執行還是要依賴IIS、Apache、Nginx等功能全面的伺服器,為ASP.NET Core程式提供類似快取、壓縮請求、SSL終端等高深的特性或功能。這兩種伺服器的關係是:Nginx、IIS等作為Kestrel的反向代理伺服器。
1.埠對映配置
#編輯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
2.驗證效果
這裡顯示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 -y install policycoreutils-python cat /var/log/audit/audit.log | grep nginx | grep denied | audit2allow -M mynginx semodule -i mynginx.pp
&n