Nginx

一個域上的 nginx 多個應用程序 https 問題與 dotnet core

  • November 10, 2020

我是 nginx 的新手。我想將 IIS 中的相同場景應用於 nginx。IIS 上有一個站點,並附有 2 個應用程序。我應用了一種方法,但我不確定它是否是正確的方法。

我正在直接繪製場景;

Site -> www.example.com
    Application(Angular) -> www example.com/app
    Application(.Net Core API) -> www.example.com/api

IIS 場景

我想在nginx中做上面提到的場景。

/etc/nginx/conf.d/example.conf ->

server {
   listen       80;
   server_name  example.com www.example.com;

   location / {
       root   /home/www/example-web;
       index  index.html;
   }

   location /app {
       alias /home/www/example-app;
       index index.html;
   }
   location /api {
       proxy_pass         http://localhost:5000;
       proxy_http_version 1.1;
       proxy_set_header   X-Real-IP        $remote_addr;
       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;
   }

   #error_page  404              /404.html;

   # redirect server error pages to the static page /50x.html
   #
   error_page   500 502 503 504  /50x.html;
   location = /50x.html {
       root   /usr/share/nginx/html;
   }
}

“wget http://www.example.com/api/values –no-check-certificate”

當我執行上面的程式碼時,我看到了;

--2020-11-07 18:06:15--  http://www.example.com/api/values
Resolving www.example.com (www.example.com)... 127.0.0.1
Connecting to www.example.com (www.example.com)|127.0.0.1|:80... connected.
HTTP request sent, awaiting response... 307 Temporary Redirect
Location: https://localhost:5001/api/values [following]
--2020-11-07 18:06:15--  https://localhost:5001/api/values
Resolving localhost (localhost)... ::1, 127.0.0.1
Connecting to localhost (localhost)|::1|:5001... connected.
WARNING: cannot verify localhost's certificate, issued by ‘/CN=localhost’:
 Unable to locally verify the issuer's authority.
HTTP request sent, awaiting response... 404 Not Found
2020-11-07 18:06:15 ERROR 404: Not Found.

為什麼重定向到 https://localhost:5001/api/values?

應該重定向到下面的地址->

http://localhost:5000/values

我能怎麼做?謝謝。

我通過將以下程式碼添加到 dotnet-my-api.service 解決了 HTTPS 問題;

Environment=ASPNETCORE_URLS=http://localhost:5000

我通過使用 nginx proxy_pass 刪除路徑解決了位置問題;

location /api/ {
       proxy_pass         http://localhost:5000/;
       ...
}

引用自:https://serverfault.com/questions/1041677