我今晚一直在嘗試配置 Nginx 以使子域正常作業,但沒有成功,我不知道如何使其正常作業,有人可以幫忙嗎
該計劃看起來像這樣
"www.example.com" points to my .net core mvc project.
"api1.example.com" points to my .net core api1 project.
"api2.example.com" points to my .net core api2 project
我的專案在我的 linux 服務器中如下列出。
mvc /var/www/example.com
api1 /var/www/api1.example.com
api2 /var/www/api2.example.com
uj5u.com熱心網友回復:
我自己弄明白了,我會在這里解釋我是如何讓它作業的。
在我最初的問題中,我提到了兩個專案 api1 和 api2,在這個解決方案中,我將只使用一個專案 api1 來簡化這個問題,
1-您需要在 A/AAAA 記錄中注冊您的子域,您在您的服務器提供商儀表板中執行此操作,我的是“linode”,
我添加的記錄是“api1”,您還將被要求提供一個 ip 地址,它是你服務器的公網IP,
2- 發布您的專案并復制生成的檔案以/var/www/api1.example.com
確保埠與 mvc 專案不同。
對我來說 mvc 專案埠是
5000。api1 專案埠是 5001。
3-為你的api1專案創建一個服務,服務檔案應該保存在下面的目錄中 /etc/systemd/system/kestrel-api1.service
[Unit]
Description=Example My ASP.NET Core Application running on Ubuntu
[Service]
WorkingDirectory=/var/www/api1.example.com
ExecStart=/usr/bin/dotnet /var/www/api1.example.com/api1.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-example
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
Environment=ASPNETCORE_URLS=http://127.0.0.1:5001
[Install]
WantedBy=multi-user.target
4- 在你的 mvc 專案的組態檔中添加一個新的服務器塊
server {
listen 80;
listen [::]:80;
server_name api1.example.com *.example.com;
root /var/www/api1.example.com;
index index.html index.htm;
location / {
proxy_pass http://localhost:5001;
}
}
5- 最后不要忘記在 SSH 終端中使用此命令重新啟動新的 api1 服務。
sudo systemctl restart kestrel-api1.service
您還需要使用此命令重新啟動 Nginx
service nginx restart
now if you navigate to example.com, the mvc project will be served.
if you navigate to api1.example.com it will serve api1 project.
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/357689.html
