linux - 从 LAN 找不到 aspNet 核心 Linux 404

标签 linux nginx asp.net-core

我在 Linux 环境 上部署了我的第一个 .netCore 应用程序。使用 Lubuntu 18.04

我首先尝试使用 apache2,但由于我在从外部访问它时遇到问题,所以我配置了 nginx 并尝试了但没有成功。

我的应用程序使用 dotnet 命令 在端口 5000 上运行,如下所示

usr:/inetpub/www/WebApi$ dotnet WebApi.dll --urls=http://:::5000/
Hosting environment: Production
Content root path: /inetpub/www/WebApi
Now listening on: http://[::]:5000
Application started. Press Ctrl+C to shut down.

这是我读取 --url 输入参数的 Program.cs 文件:

public class Program
{
    public static void Main(string[] args)
    {

        XmlDocument log4netConfig = new XmlDocument();
        log4netConfig.Load(File.OpenRead("log4net.config"));
        ILoggerRepository repo = LogManager.CreateRepository(Assembly.GetEntryAssembly(),
                   typeof(log4net.Repository.Hierarchy.Hierarchy));
        log4net.Config.XmlConfigurator.Configure(repo, log4netConfig["log4net"]);

        //CreateWebHostBuilder(args).Build().Run();


        if (args != null && args.Count() > 0)
        {

            var configuration = new ConfigurationBuilder()
                .AddCommandLine(args)
                .Build();

            var host = new WebHostBuilder()
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseConfiguration(configuration)
                .UseIISIntegration()
                .UseStartup<Startup>()
                .Build();

            host.Run();
        }
        else
        {
            var host = new WebHostBuilder()
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration()
                .UseStartup<Startup>()
                .UseUrls("http://*:8080/")
                .Build();

            host.Run();
        }
    }
}

这是我在 nginx 的可用站点文件夹 中的默认 文件。

server {
    listen        80;
    server_name  _;
    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;
    }
}

这是我的nginx.conf 文件

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;
        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}


#mail {
#       # See sample authentication script at:
#       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#       # auth_http localhost/auth.php;
#       # pop3_capabilities "TOP" "USER";
#       # imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#       server {
#               listen     localhost:110;
#               protocol   pop3;
#               proxy      on;
#       }
# 
#       server {
#               listen     localhost:143;
#               protocol   imap;
#               proxy      on;
#       }
#}

这是我的 WebApi 核心 Startup.cs 文件

公开课启动 { public Startup(IConfiguration配置) { 配置=配置;

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

        DapperExtensions.DapperExtensions.SqlDialect = new DapperExtensions.Sql.MySqlDialect();

        ConnectionString connectionString = new ConnectionString();
        connectionString._ConnectionString = new Parameters.AppSettingsParameter().getConnectionString();

        services.AddSingleton<IConnectionString>(connectionString);

        services.AddScoped<ICustomerRepository>(x => new Infrastructure.Dapper.EntitiesRepository.CustomerRepository(connectionString));
        services.AddScoped<IDeviceRepository>(x => new Infrastructure.Dapper.EntitiesRepository.DeviceRepository(connectionString));
        services.AddScoped<IWebApiVideoRepository>(x => new Infrastructure.Dapper.EntitiesRepository.WebApiVideoRepository(connectionString));
        services.AddScoped<IMessageServiceTokenRepository>(x => new Infrastructure.Dapper.EntitiesRepository.MessageServiceTokenRepository(connectionString));
        services.AddScoped<IPriceRepository>(x => new Infrastructure.Dapper.EntitiesRepository.PriceRepository(connectionString));
        services.AddScoped<IServiceRepository>(x => new Infrastructure.Dapper.EntitiesRepository.ServiceRepository(connectionString));
        services.AddScoped<IWebApiVideoDownloadFromDeviceRepository>(x => new Infrastructure.Dapper.EntitiesRepository.WebApiVideoDownloadFromDeviceRepository(connectionString));
        services.AddScoped<IWebApiVideoValidationRefusedRepository>(x => new Infrastructure.Dapper.EntitiesRepository.WebApiVideoValidationRefusedRepository(connectionString));
        services.AddScoped<ITokenKeyRepository>(x => new Infrastructure.Dapper.EntitiesRepository.TokenKeyRepository(connectionString));
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseCookiePolicy();

        app.UseMiddleware<RequestResponseLoggingMiddleware>();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Init}/{action=Initialize}");
        });
    }
}

如果我转到 localhost,我可以 ping 通在 5000 端口上运行的应用程序。

从另一台计算机转到 192.168.1.46(我的 linux pc 地址)得到 404 错误页面

这是nmap命令的结果:

PORT   STATE SERVICE
80/tcp open  http

这是我的 iptable -L 命令:

Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:http
2    ufw-before-logging-input  all  --  anywhere             anywhere            
3    ufw-before-input  all  --  anywhere             anywhere            
4    ufw-after-input  all  --  anywhere             anywhere            
5    ufw-after-logging-input  all  --  anywhere             anywhere            
6    ufw-reject-input  all  --  anywhere             anywhere            
7    ufw-track-input  all  --  anywhere             anywhere            
8    ACCEPT     all  --  anywhere             anywhere            
9    ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:http

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         
1    ufw-before-logging-forward  all  --  anywhere             anywhere            
2    ufw-before-forward  all  --  anywhere             anywhere            
3    ufw-after-forward  all  --  anywhere             anywhere            
4    ufw-after-logging-forward  all  --  anywhere             anywhere            
5    ufw-reject-forward  all  --  anywhere             anywhere            
6    ufw-track-forward  all  --  anywhere             anywhere            

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ufw-before-logging-output  all  --  anywhere             anywhere            
2    ufw-before-output  all  --  anywhere             anywhere            
3    ufw-after-output  all  --  anywhere             anywhere            
4    ufw-after-logging-output  all  --  anywhere             anywhere            
5    ufw-reject-output  all  --  anywhere             anywhere            
6    ufw-track-output  all  --  anywhere             anywhere            

Chain ufw-after-forward (1 references)
num  target     prot opt source               destination         

Chain ufw-after-input (1 references)
num  target     prot opt source               destination         

Chain ufw-after-logging-forward (1 references)
num  target     prot opt source               destination         

Chain ufw-after-logging-input (1 references)
num  target     prot opt source               destination         

Chain ufw-after-logging-output (1 references)
num  target     prot opt source               destination         

Chain ufw-after-output (1 references)
num  target     prot opt source               destination         

Chain ufw-before-forward (1 references)
num  target     prot opt source               destination         

Chain ufw-before-input (1 references)
num  target     prot opt source               destination         

Chain ufw-before-logging-forward (1 references)
num  target     prot opt source               destination         

Chain ufw-before-logging-input (1 references)
num  target     prot opt source               destination         

Chain ufw-before-logging-output (1 references)
num  target     prot opt source               destination         

Chain ufw-before-output (1 references)
num  target     prot opt source               destination         

Chain ufw-reject-forward (1 references)
num  target     prot opt source               destination         

Chain ufw-reject-input (1 references)
num  target     prot opt source               destination         

Chain ufw-reject-output (1 references)
num  target     prot opt source               destination         

Chain ufw-track-forward (1 references)
num  target     prot opt source               destination         

Chain ufw-track-input (1 references)
num  target     prot opt source               destination         

Chain ufw-track-output (1 references)
num  target     prot opt source               destination         

这是我的 netstat 命令:

Proto CodaRic CodaInv Indirizzo locale        Indirizzo remoto       Stato       PID/Program name    
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      21391/mysqld        
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      19096/nginx: master 
tcp        0      0 0.0.0.0:55250           0.0.0.0:*               LISTEN      17341/anydesk       
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      738/systemd-resolve 
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      29185/cupsd         
tcp        0      0 0.0.0.0:7070            0.0.0.0:*               LISTEN      17341/anydesk       
tcp6       0      0 :::5000                 :::*                    LISTEN      19464/dotnet        
tcp6       0      0 :::80                   :::*                    LISTEN      19096/nginx: master 
tcp6       0      0 :::21                   :::*                    LISTEN      1037/vsftpd         
tcp6       0      0 ::1:631                 :::*                    LISTEN      29185/cupsd         
udp        0      0 0.0.0.0:60895           0.0.0.0:*                           938/avahi-daemon: r 
udp        0      0 127.0.0.53:53           0.0.0.0:*                           738/systemd-resolve 
udp        0      0 0.0.0.0:68              0.0.0.0:*                           1691/dhclient       
udp        0      0 0.0.0.0:631             0.0.0.0:*                           29186/cups-browsed  
udp        0      0 224.0.0.251:5353        0.0.0.0:*                           29228/chrome        
udp        0      0 224.0.0.251:5353        0.0.0.0:*                           29228/chrome        
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           938/avahi-daemon: r 
udp6       0      0 :::39611                :::*                                938/avahi-daemon: r 
udp6       0      0 :::5353                 :::*                                938/avahi-daemon: r 

这是此命令的日志:sudo tcpdump -i any tcp port 80 当我尝试从局域网中的另一台电脑调用我的 ip 时:

00:06:31.785311 IP 192.168.1.44.63326 > WebApi.http: Flags [F.], seq 1, ack 1, win 256, length 0
00:06:31.785407 IP WebApi.http > 192.168.1.44.63326: Flags [F.], seq 1, ack 2, win 229, length 0
00:06:31.785599 IP 192.168.1.44.63362 > WebApi.http: Flags [S], seq 1225666604, win 64240, options [mss 1460,nop,wscale 8,nop,nop,sackOK], length 0
00:06:31.785635 IP WebApi.http > 192.168.1.44.63362: Flags [S.], seq 4261901272, ack 1225666605, win 29200, options [mss 1460,nop,nop,sackOK,nop,wscale 7], length 0
00:06:31.787248 IP 192.168.1.44.63327 > WebApi.http: Flags [P.], seq 461:921, ack 138, win 256, length 460: HTTP: GET / HTTP/1.1
00:06:31.787272 IP WebApi.http > 192.168.1.44.63327: Flags [.], ack 921, win 245, length 0
00:06:31.788867 IP WebApi.http > 192.168.1.44.63327: Flags [P.], seq 138:275, ack 921, win 245, length 137: HTTP: HTTP/1.1 404 Not Found
00:06:31.790175 IP 192.168.1.44.63326 > WebApi.http: Flags [.], ack 2, win 256, length 0
00:06:31.790513 IP 192.168.1.44.63362 > WebApi.http: Flags [.], ack 1, win 256, length 0
00:06:31.832376 IP 192.168.1.44.63327 > WebApi.http: Flags [.], ack 275, win 255, length 0

我正在努力解决这个问题,我不知道我到底该怎么做才能让它发挥作用。 我唯一可以说的是,如果我的 dotnet 应用程序正在运行,我会收到 404 错误如果它没有运行,我会收到 502 Bad Gateway 错误。

我该怎么做才能让它发挥作用?

PS:我把我想到的都加进去了,如果有遗漏的地方,欢迎追问实现

谢谢大家

最佳答案

不知何故,我假设文件在发布过程中损坏了;我删除并复制回了我的 .netCore 项目的所有文件,一切开始正常工作。

也就是说,我会保留这个问题,因为我认为它共享一些可能对其他人有用的配置,因为在这一点上我认为这些是正确的:)

谢谢大家的支持

关于linux - 从 LAN 找不到 aspNet 核心 Linux 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55347704/

相关文章:

angular - Angular SPA 中的 OpenID Connect 与 Asp.Net Core 3.1 后端

linux - 按列排序 Unix

linux - ./!$ 在 Linux 中是什么意思?

node.js - 仅在 Linux 上将原始图像字节馈送到 ffmpeg rawvideo 失败,缓冲区大小无效

javascript - 获取请求的客户端 IP 地址而不是 Cloudflare 的 IP 地址

web-applications - Go Webapp & Nginx : Confusion about listening, fastcgi & 反向代理

regex - unix:如何判断字符串是否与正则表达式匹配

nginx - ffmpeg 转发流有 3 秒延迟

c# - JSonConverter 如何进行泛型反序列化

c# - 当@Model 是任务时访问@Model