【问题标题】:Remote Access to Local ASP.NET Core app NGINX远程访问本地 ASP.NET Core 应用 NGINX
【发布时间】:2020-03-23 12:41:10
【问题描述】:

我使用 nginx 在我的 raspberrypi3 (raspbian) 上发布了我的 ASP.NET Core 应用程序。

我按照 microsoft 文档配置了 nginx:在 localhost 上一切正常,但我无法从本地网络上的其他设备访问应用程序 (ERR_CONN_REFUSED)。

我在端口 81 上设置了反向代理,因为在端口 80 我有另一台服务器管理 php 站点(包括 phpmyadmin),如下所示:

server {
listen 80 default_server;
listen [::]:80;

root /var/www/html;

index index.php index.html index.htm index.nginx-debian.html;

server_name php.it *.php.it;

location / {

    try_files $uri $uri/ =404;
}


location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.3-fpm.sock;
}}

server {
listen 81;

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;
}}

不知道问题出在/etc/nginx/sites-available/default文件的配置上还是在asp.net Core应用程序上。 我认为问题是本地主机,但我不知道如何解决它。

另外,我不确定我应该在 server_sites 中放什么,它指的是什么? 谢谢

【问题讨论】:

  • server_name 正确吗?您是否像这样正确访问它example.com:81
  • @M1K1O 感谢您的回答,我认为这是错误的。我的意思是,我不知道我应该在server_name 中写什么以及如何设置它。实际上,如果我搜索example.com:81,我会得到ERR_CONNECTION_TIMED_OUT。我应该如何设置它?
  • 如果您的域和 DNS 设置正确,请将其添加到此处。如果你只想通过 IP 访问它,比如172.28.0.1:81remove。
  • @M1K1O 好的,我只是想通过 ip 访问它,所以我删除了它。在本地主机中它可以工作,但从另一个设备我仍然得到ERR_CONNECTION_REFUSED。如果 nginx 配置没有问题,那么我认为这是一个应用程序问题。我是否需要在 program.cs 中设置某些内容才能访问 localhost 之外的应用程序?
  • 如果您的应用程序在http://localhost:5000 可用,那么应用程序设置正确。

标签: c# asp.net-core nginx .net-core


【解决方案1】:

在您的第一个配置中,您已经设置了域 php.it,因此如果您设置了正确的 DNS,您的网站应该可以通过 http://php.it/ 访问。

但是你已经设置了收听default_server,所以它也可以在没有主机的情况下访问。

在您的第二种情况下,缺少default_server,因此主机是必需的。 example.com:81 应该是您的页面可访问,如果您设置了正确的 DNS。

解决方案:

  • 如果你想访问没有域名的网站,只是纯IP,删除server_name
  • 如果你想为81端口设置默认服务器,添加listen 81 default_server;

【讨论】:

    猜你喜欢
    • 2019-10-12
    • 2020-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-12
    • 1970-01-01
    • 2019-11-05
    相关资源
    最近更新 更多