The original post: /r/nginx by /u/allnnde on 2024-07-12 22:23:33.
Hi, I hope you can help me
I currently have a system consisting of:
I am using a backend in .net and a frontend in react.
when I raise everything in a container I have the backend container and the nginx container which has in the html folder the react files.
now I try to add in the nginx configuration the path to see the swagger of the backend, but it is always throwing me an error.
the last error that I have thrown is end of the stream or a document separator is expected
nginx.conf:
events{
worker_connections 768;
}
http{
server{
listen 80 default_server;
listen [::]:80 default_server;
server_name doctaonline.com, www.doctaonline.com;
root /usr/share/nginx/html;
index index.html index.htm;
include /etc/nginx/mime.types;
try_files $uri /index.html =404;
location / {
try_files $uri $uri/ /index.html;
}
location /api {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://webapi:80;
}
location /docs {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://webapi:80/swagger/index.html;
}
location /health {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://webapi:80/health;
}
}
}
startup.cs
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
var corsPolicyName = "_corsPolicy";
builder.Host.UseFileLogging(builder.Configuration);
// Add services to the container.
builder.Services.AddControllers()
.AddJsonOptions(opt => opt.JsonSerializerOptions.ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.IgnoreCycles);
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(option =>
{
option.SwaggerDoc("v1", new OpenApiInfo { Title = "Docta API", Version = "v1" });
option.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
In = ParameterLocation.Header,
Description = "Please enter a valid token",
Name = "Authorization",
Type = SecuritySchemeType.Http,
BearerFormat = "JWT",
Scheme = "Bearer"
});
option.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type=ReferenceType.SecurityScheme,
Id="Bearer"
}
},
new string[]{}
}
});
});
builder.Services.AddHttpContextAccessor();
builder.Services.Configure<ApiBehaviorOptions>(options =>
{
options.SuppressModelStateInvalidFilter = true;
});
builder.Services.AddCors(opt =>
{
opt.AddPolicy(corsPolicyName, plc => plc.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
});
builder.Services.AddAuthorization()
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = false,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = builder.Configuration["Auth:Issuer"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["Auth:Key"]))
};
});
//Add custom Serivces
builder.Services.RegisterDbContext(builder.Configuration);
builder.Services.RegisterRepositories();
builder.Services.RegisterServices();
builder.Services.RegisterHelpers();
builder.Services.RegisterAutomapper();
builder.Services.RegisterValidators();
builder.Services.AddHealthChecks()
.AddCheck<EnviromentHealthCheck>("Environment")
.AddCheck<VersionHealthCheck>("Version")
.AddCheck<PaymentUrlHealthCheck>("PaymentURL")
.AddNpgSql(builder.Configuration.GetConnectionString("ELEARMING"));
var app = builder.Build();
// Configure the HTTP request pipeline.
app.UseSwagger();
app.UseSwaggerUI();
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
//app.UseHttpsRedirection();
app.UseCors(corsPolicyName);
app.UseRouting();
app.UseDefaultFiles();
app.UseStaticFiles(new StaticFileOptions
{
ServeUnknownFileTypes = true
});
app.UseAuthorization();
app.UseHealthChecks("/health", new HealthCheckOptions()
{
Predicate = _ => true,
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
});
app.MapControllers();
app.MapHealthChecks("/health");
app.Run();
}
}
Soory for my english
You must log in or register to comment.