V
V
Vellis-msk2020-09-16 17:14:51
Nginx
Vellis-msk, 2020-09-16 17:14:51

How to proxy to ASP.NET core via nginx?

Good afternoon.
There is a task to host several ASP.NET Core web services on the same Centos server.
The services themselves have been successfully created and are working.
Service configuration example 1:

[Unit]
Description=Test service 1
[Service]
WorkingDirectory=/home/www/test1
ExecStart=/usr/bin/dotnet /home/www/test1/test1.dll--urls "http://*:5002"
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-example
User=root
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
[Install]
WantedBy=multi-user.target

Service config 2:
[Unit]
Description=Test service 2
[Service]
WorkingDirectory=/home/www/test2
ExecStart=/usr/bin/dotnet /home/www/test2/test2.dll--urls "http://*:5003"
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-example
User=root
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
[Install]
WantedBy=multi-user.target

Everything is great and wonderful, if you access these services using the example of net-core.lan:5002 and net-core.lan:5003 - no problem.
However, the task is in such a way that it is necessary for this server to publish port 443 outside, fasten the certificate and proxy calls to services through nginx.
Create nginx configuration:
user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


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

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/proxy.conf;
}

And in /etc/nginx/conf.d/ we create our custom config for services:
server {
    client_header_buffer_size 1k;
    large_client_header_buffers 2 1k;
    #Listening port:
    listen     80;
    listen     443 ssl;
    #Server names:
    server_name  domain.com;
    #SSL oprions:
    ssl_certificate /etc/ssl/domain_com.crt;
    ssl_certificate_key     /etc/ssl/private.key;
    #Locations config:
location /test1 {
    # redirect all HTTP traffic to localhost:5002
    proxy_pass http://127.0.0.1:5002;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    # WebSocket support
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}
location /test2 {
    # redirect all HTTP traffic to localhost:5003
    proxy_pass http://127.0.0.1:5003;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    # WebSocket support
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}
}

Thus, when we access domain.com/test1, our request should go to 127.0.0.1:5002 and be processed by the service, but judging by the nginx logs, the request is 127.0.0.1:5002/test and therefore the service does not work properly.
What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wexter, 2020-09-16
@Vellis-msk

location nginx forwards as is, i.e. if you have location / test1, then requests will come there from url / test1.
You can add rewrite so that it removes your location from the url, or make 2 different domains and directly proxy location / to the necessary services.
rewrite will be something like this

rewrite ^/test1/(.*) /$1 break;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question