301循环启用使用NGINX在NOPCommerce上的SSL 4.50
我有一个正在运行的 Nginx 安装,它已成功地通过 SSL 提供了 index.html,没有任何问题。
当我将其指向同一台计算机上的 nopCommerce 4.50 站点时,nopcommerce 站点现在可以使用 SSL。但是,页面上的所有链接以及资源仍然使用 http,并且 Firefox 会发出警告“此页面的部分内容不安全”。
为了尝试解决此问题,我更改了 url 并在 nop 设置中启用了 SSL。
当我这样做时,该网站现在无限地重定向到自身。例如,访问 https://mynopcommerce.com
将返回 301 重定向到 https://mynopcommerce.com
。为了让网站再次运行,我必须在 nop 数据库中手动禁用 SSL。
- 我设置了“UseHttpXForwardedProto”在 appsettings.json 中设置为 true
- 我已经清除了浏览器/服务器/代理 cookie 和缓存。
- 我没有使用 cloudflare、dns 或除 nginx 之外的任何其他代理服务作为反向代理。
我的 nginx 服务器块:
server {
listen 443 ssl;
server_name mynopcommerce.com;
ssl_certificate /etc/letsencrypt/live/mynopcommerce.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mynopcommerce.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
location / {
proxy_pass http://nopcommerce_web; # DNS resolves name to nop server
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 处理 SSL 有关。并且在 nop 上启用 SSL 后,nop 和 nginx 之间的非 ssl 通信可能会使 nop 返回 301 到 https 版本的站点,而不知道它已经在上面了?
这是 1 个请求期间 nginx 和 nop 的日志。 (它循环这个直到出错)
nopcommerce_nginx | [03/Apr/2022:21:07:00 +0000] "GET / HTTP/1.1" 301 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:98.0) Gecko/20100101 Firefox/98.0" "-"
nopcommerce_web | {"EventId":1,"LogLevel":"Information","Category":"Microsoft.AspNetCore.Hosting.Diagnostics","Message":"Request starting HTTP/1.1 GET http://mynopcommerce.com/ - -","State":{"Message":"Request starting HTTP/1.1 GET http://mynopcommerce.com/ - -","Protocol":"HTTP/1.1","Method":"GET","ContentType":null,"ContentLength":null,"Scheme":"http","Host":"mynopcommerce.com","PathBase":"","Path":"/","QueryString":""}}
nopcommerce_web | {"EventId":0,"LogLevel":"Information","Category":"Microsoft.AspNetCore.Routing.EndpointMiddleware","Message":"Executing endpoint \u0027Nop.Web.Controllers.HomeController.Index (Nop.Web)\u0027","State":{"Message":"Executing endpoint \u0027Nop.Web.Controllers.HomeController.Index (Nop.Web)\u0027","EndpointName":"Nop.Web.Controllers.HomeController.Index (Nop.Web)","{OriginalFormat}":"Executing endpoint \u0027{EndpointName}\u0027"}}
nopcommerce_web | {"EventId":3,"LogLevel":"Information","Category":"Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker","Message":"Route matched with {action = \u0022Index\u0022, controller = \u0022Home\u0022, area = \u0022\u0022}. Executing controller action with signature Microsoft.AspNetCore.Mvc.IActionResult Index() on controller Nop.Web.Controllers.HomeController (Nop.Web).","State":{"Message":"Route matched with {action = \u0022Index\u0022, controller = \u0022Home\u0022, area = \u0022\u0022}. Executing controller action with signature Microsoft.AspNetCore.Mvc.IActionResult Index() on controller Nop.Web.Controllers.HomeController (Nop.Web).","RouteData":"{action = \u0022Index\u0022, controller = \u0022Home\u0022, area = \u0022\u0022}","MethodInfo":"Microsoft.AspNetCore.Mvc.IActionResult Index()","Controller":"Nop.Web.Controllers.HomeController","AssemblyName":"Nop.Web","{OriginalFormat}":"Route matched with {RouteData}. Executing controller action with signature {MethodInfo} on controller {Controller} ({AssemblyName})."}}
nopcommerce_web | {"EventId":3,"LogLevel":"Information","Category":"Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker","Message":"Authorization failed for the request at filter \u0027Nop.Web.Framework.Mvc.Filters.HttpsRequirementAttribute\u002BHttpsRequirementFilter\u0027.","State":{"Message":"Authorization failed for the request at filter \u0027Nop.Web.Framework.Mvc.Filters.HttpsRequirementAttribute\u002BHttpsRequirementFilter\u0027.","AuthorizationFilter":"Nop.Web.Framework.Mvc.Filters.HttpsRequirementAttribute\u002BHttpsRequirementFilter","{OriginalFormat}":"Authorization failed for the request at filter \u0027{AuthorizationFilter}\u0027."}}
I have a working Nginx installation that has successfully served an index.html with SSL with no problems.
When I point it toward my nopCommerce 4.50 site on the same machine, the nopcommerce site now works with SSL. However, all links on the page as well as resources still use http and firefox gives a warning that "Parts of this page are not secure"
To attempt to fix that, I have changed the url and enabled SSL in the nop settings.
When I did that, the site now infinitely redirects to itself. For instance, accessing https://mynopcommerce.com
returns a 301 redirecting to https://mynopcommerce.com
. To get the site working again, I have to manually disable SSL in the nop database.
I have tried all fixes for this issue as suggested by https://docs.nopcommerce.com/en/getting-started/advanced-configuration/how-to-install-and-configure-ssl-certification.html#troubleshooting
- I have set "UseHttpXForwardedProto" to true in the appsettings.json
- I have cleared browser/server/proxy cookies and cache.
- I am not using cloudflare, dns, or any other proxy services other than nginx as the reverse proxy.
My nginx server block:
server {
listen 443 ssl;
server_name mynopcommerce.com;
ssl_certificate /etc/letsencrypt/live/mynopcommerce.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mynopcommerce.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
location / {
proxy_pass http://nopcommerce_web; # DNS resolves name to nop server
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;
}
}
I believe it might be related to the fact that nginx is handling SSL. And with SSL enabled on nop, the non-ssl communication between nop and nginx may make nop return a 301 to the https version of the site, not knowing that it's already on it?
This is the log from nginx and nop during 1 request. (It loops this until error)
nopcommerce_nginx | [03/Apr/2022:21:07:00 +0000] "GET / HTTP/1.1" 301 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:98.0) Gecko/20100101 Firefox/98.0" "-"
nopcommerce_web | {"EventId":1,"LogLevel":"Information","Category":"Microsoft.AspNetCore.Hosting.Diagnostics","Message":"Request starting HTTP/1.1 GET http://mynopcommerce.com/ - -","State":{"Message":"Request starting HTTP/1.1 GET http://mynopcommerce.com/ - -","Protocol":"HTTP/1.1","Method":"GET","ContentType":null,"ContentLength":null,"Scheme":"http","Host":"mynopcommerce.com","PathBase":"","Path":"/","QueryString":""}}
nopcommerce_web | {"EventId":0,"LogLevel":"Information","Category":"Microsoft.AspNetCore.Routing.EndpointMiddleware","Message":"Executing endpoint \u0027Nop.Web.Controllers.HomeController.Index (Nop.Web)\u0027","State":{"Message":"Executing endpoint \u0027Nop.Web.Controllers.HomeController.Index (Nop.Web)\u0027","EndpointName":"Nop.Web.Controllers.HomeController.Index (Nop.Web)","{OriginalFormat}":"Executing endpoint \u0027{EndpointName}\u0027"}}
nopcommerce_web | {"EventId":3,"LogLevel":"Information","Category":"Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker","Message":"Route matched with {action = \u0022Index\u0022, controller = \u0022Home\u0022, area = \u0022\u0022}. Executing controller action with signature Microsoft.AspNetCore.Mvc.IActionResult Index() on controller Nop.Web.Controllers.HomeController (Nop.Web).","State":{"Message":"Route matched with {action = \u0022Index\u0022, controller = \u0022Home\u0022, area = \u0022\u0022}. Executing controller action with signature Microsoft.AspNetCore.Mvc.IActionResult Index() on controller Nop.Web.Controllers.HomeController (Nop.Web).","RouteData":"{action = \u0022Index\u0022, controller = \u0022Home\u0022, area = \u0022\u0022}","MethodInfo":"Microsoft.AspNetCore.Mvc.IActionResult Index()","Controller":"Nop.Web.Controllers.HomeController","AssemblyName":"Nop.Web","{OriginalFormat}":"Route matched with {RouteData}. Executing controller action with signature {MethodInfo} on controller {Controller} ({AssemblyName})."}}
nopcommerce_web | {"EventId":3,"LogLevel":"Information","Category":"Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker","Message":"Authorization failed for the request at filter \u0027Nop.Web.Framework.Mvc.Filters.HttpsRequirementAttribute\u002BHttpsRequirementFilter\u0027.","State":{"Message":"Authorization failed for the request at filter \u0027Nop.Web.Framework.Mvc.Filters.HttpsRequirementAttribute\u002BHttpsRequirementFilter\u0027.","AuthorizationFilter":"Nop.Web.Framework.Mvc.Filters.HttpsRequirementAttribute\u002BHttpsRequirementFilter","{OriginalFormat}":"Authorization failed for the request at filter \u0027{AuthorizationFilter}\u0027."}}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从 4.50 版本开始,nopCommerce 更改了此 API。您应该使用
appSettings.json
文件中的UseProxy
选项(设置为true
值),而不是UseHttpXForwardedProto
。Since version 4.50, nopCommerce has changed this API. You should use the
UseProxy
option (set totrue
value) inappSettings.json
file instead of theUseHttpXForwardedProto
.您可以清除 ForwardedHeadersOptions 中的 KnownNetworks 和 KnownProxies 或将 ASPNETCORE_FORWARDEDHEADERS_ENABLED 设置为 true ,以执行 相同。查看更多此处。这个答案也可以提供帮助。
You can clear KnownNetworks and KnownProxies in ForwardedHeadersOptions or set ASPNETCORE_FORWARDEDHEADERS_ENABLED to true that does the same. See more here. This answer also can help.
有很多事情可能会导致这个问题,但我想指出一个违反直觉的特定修复(以及我在 Traefik 后面运行 nopCommerce 4.7.3 时遇到的问题:
当使用终止 SSL 连接的反向代理时并通过 HTTP 连接到后端,您需要将
UseProxy
设置为“true”并保留KnownProxies
空白如果您在此配置中的KnownProxies
中放置一个值,则该网站将表现得需要提供自己的 https 内容并导致无限循环的重定向。这是我的 appsettings.json 中的相关部分供参考:
我在这个答案<的倒数第二段中找到了关键信息。 /a>
There are quite a few things that can cause this issue, but I want to point out one particular fix that's counter-intuitive (and the problem I had running nopCommerce 4.7.3 behind Traefik:
When using a reverse-proxy that terminates the SSL connection and connects to the backend via HTTP, you need to set
UseProxy
to "true" and leaveKnownProxies
blank in appsettings.json. If you put a value inKnownProxies
in this configuration, the site will act like it needs to serve its own https content and cause an endless loop of redirects.Here's the relevant section from my appsettings.json for reference:
I found the key piece of information in the second-to-last paragraph of this answer