---恢復內容開始---
前兩篇文章介紹了使用Docker作為運行環境,用Nginx做反向代理的部署方法,這篇介紹一下使用Nginx配合.Net Core運行時直接在Liunx上裸奔的方法。
一、安裝.NET Core 運行時
如果已經安裝了運行時,請跳過第一步。
https://www.microsoft.com/net/download/linux-package-manager/rhel/runtime-current
在網址中下載對應的操作系統運行時,我使用的是CentOs7-x64
二、配置服務
在/etc/systemd/system 下新建文件(推薦使用winscp),文件名以.service結尾,配置內容為:
[Unit] Description=HZKJ #服務描述,隨便填就好 [Service] WorkingDirectory=/website/HZKJ #工作目錄,填你應用的絕對路徑 ExecStart=/usr/bin/dotnet /website/HZKJ/CZKJ.CMS.Web.dll #啟動:前半截是你dotnet的位置(一般都在這個位置),后半部分是你程序入口的dll,中間用空格隔開 Restart=always RestartSec=25 #如果服務出現問題會在25秒后重啟,數值可自己設置 SyslogIdentifier=HZKJ #設置日志標識,此行可以沒有 User=root #配置服務用戶,越高越好 Environment=ASPNETCORE_ENVIRONMENT=Production [Install] WantedBy=multi-user.target
寫完配置文件后保存,輸入指令確認服務:
systemctl enable (你的服務名).service
例如:systemctl enable HZKJ.service;
然后啟動服務 systemctl start HZKJ.service
然后查看一下服務狀態:systemctl status HZKJ 出現下圖狀態說明服務運行正常。
三、配置Nginx反向代理
首先你得先安裝Nginx,沒有安裝的同學請參考前兩篇文章。
在 /etc/nginx下找到Nginx配置文件 nginx.conf 打開配置文件
找到Server節點按照一下配置進行配置
server {
listen 80 ; #監聽80端口
server_name www.hongzhuojituan.com; #綁定域名
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf; #指定配置文件可不填
#配置反向代理
location / {
proxy_pass http://localhost:5000; #把80端口的訪問反向代理到5000端口(你程序的啟動端口),請根據實際情況修改自己的端口
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
#配置沒有帶www時把請求301到有www的域名
server {
listen 80 ;
server_name hongzhuojituan.com;
rewrite ^(.*)$ http://www.hongzhuojituan.com$1 permanent;
root /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf;
}
精簡版本:
server { listen 80;
server_name example.com *.example.com;
location / {
proxy_pass http://localhost:5000;
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;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
配置好之后保存,輸入命令:nginx -t 檢測配置文件是否有錯
如果沒有錯的話輸入命令:nginx -s reload 讓nginx 加載最新的配置文件
在外界訪問域名,成功訪問到在5000端口運行的程序。
至此在使用Nginx進行反向代理成功,后期如果更改了程序內容需要重啟對應服務。