我是 nginx 新手。我想将 IIS 中的相同场景应用到 nginx。IIS 上有一个站点,其中附加了 2 个应用程序。我应用了一种方法,但不确定它是否正确。
我正在直接绘制场景;
Site -> www.example.com
Application(Angular) -> www example.com/app
Application(.Net Core API) -> www.example.com/api
我想在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;
}
}
“wgethttp://www.example.com/api/values--无检查证书”
当我运行上述代码时,我看到了它;
--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/值
我该怎么办?谢谢。
答案1
我通过在 dotnet-my-api.service 中添加以下代码解决了 HTTPS 问题;
Environment=ASPNETCORE_URLS=http://localhost:5000
我通过使用 nginx proxy_pass 删除路径解决了位置问题;
location /api/ {
proxy_pass http://localhost:5000/;
...
}