0

I deployed my first .netCore application on Linux environment. Using Lubuntu 18.04.

I tried first with apache2, but since I had a problem reaching it from outside, I configured nginx and tried without much success to do it.

My application is running on port 5000 with dotnet command, as follow

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.

And this is the Program.cs file where I read for the --url input parameter:

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

This is my default file inside nginx's sites-available folder.

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

This is my nginx.conf file

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

This is my WebApi Core Startup.cs file

public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; }

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

If I go to localhost, I can ping the application running on 5000 port.

Going from another computer to 192.168.1.46 (my linux pc's address) gets the 404 error page.

This is the result from nmap command:

PORT   STATE SERVICE
80/tcp open  http

this is my iptable -L command:

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         

This is my netstat command:

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 

This is the log from this command: sudo tcpdump -i any tcp port 80 when I try to call my ip from another pc in LAN:

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

I'm struggling on that and I can't figure out how the hell I can make it work. The only thing I can say is that if my dotnet application is running, I get the 404 error. If it's not running I get the 502 Bad Gateway error.

What the hell can I do to make it work?

PS: I added everything I thought at, if it misses something, feel free to ask for implementations

Thanks you all

4
  • Can you rich 80 port from localhost? Commented Mar 25, 2019 at 23:26
  • Yes, but only from localhost @Alexander Commented Mar 26, 2019 at 7:04
  • I tried your configuration above. Since I don't have all your ASP.NET Core codes, so I use I own project. And also I don't have an IPv6 enabled linux server , so I use an IPv4 configuration instead (namely make the kestrel listen on --urls=http://0.0.0.0:5000). Your nginx configuration works fine for me if I use --urls=http://0.0.0.0:5000. Could you please try that? Commented Mar 26, 2019 at 11:01
  • 1
    @itminus I tried to delete all my .netCore project's files and I copied them all back and it started to work. Probably a file got corrupted or something similar during the first copy.. Awkard. Thanks anyway! Commented Mar 27, 2019 at 9:11

1 Answer 1

1

Somehow I suppose a file got corrupted during the publish process; I deleted and copied back all files of my .netCore project and things started to work.

That said, I will keep this question since I think it shares some configurations that might be useful to someone else, since at this point I suppose those are correct :)

Thanks anyway for the support

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.