Answer the question
In order to leave comments, you need to log in
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
[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
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;
}
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";
}
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question