英文:
ASP.Net Core 2.2 deployed on Linux via NGINX, POST requests returning HTTP Error 400
问题
I am deploying a simple asp.net core app that uses only the home controller and a single post method. We have two internal networks, one consisting of only Windows machines and the other consisting of only Linux machines. The asp core app has been deployed via NGINX on the Linux side. To clarify, I can view the website from the Windows side through the Linux Login node's IP Address 10.10.40.50:8081 as the URL. To view the website from the Linux side on the machine hosting the site, it is just localhost:5000.
The post method simply takes a single input and returns a corresponding PDF file to the input, and is as follows:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(string label) {
if (ModelState.IsValid && label.Length > 0) {
string newLabel = label.Trim().Split(' ')[0].ToUpper();
if (fstickChecker.Contains(newLabel)) {
var pdf = await CreatePDF(newLabel);
return pdf;
} else {
return RedirectToAction("Index", new RouteValueDictionary(new { action = "Index", err = "Invalid label selected" }));
}
}
return View();
}
The Razor code:
<form method="post" action="/Home/Create">
@Html.AntiForgeryToken()
<input size="60" type="text" name="label" class="form-control" id="labeler" placeholder="Enter Label" style="float:left;">
<div class="rotator-buttons" style="padding-top:45px;">
<button type="submit" class=" btn btn-01 t-white" style="float:left; height:40px;width:140px;background-color:#706552">Create PDF</button>
</div>
</form>
The post method operates correctly when run on a Windows machine via the VS 2017 debugger and from the Linux machine hosting it (i.e., when using the localhost:5000 address).
However, it fails when viewing the site from the Windows side (i.e., the 10.10.40:8081).
There is a GET method (named Search) that provides Search Engine type functionality in the textbox where the label is entered that operates correctly under all circumstances, so it appears that only the POST method is failing for whatever reason.
My Program.cs
portion:
public class Program {
public static void Main(string[] args) {
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
My Startup.cs
file:
public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
app.UseStaticFiles();
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
} else {
app.UseExceptionHandler("/Home/Error");
}
//app.UseCookiePolicy();
app.UseMvc(routes => {
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
The NGINX sites-available
file (where I think the issue is probably coming from) is:
server {
listen 8081 default_server;
listen [::]:8081 default_server;
root /var/www/html;
# Other NGINX configuration settings...
location / {
proxy_pass http://localhost:5000/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /Home/ {
proxy_pass http://localhost:5000/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
I have found some related Stack Overflow questions, but none of them were able to fix my problem:
英文:
I am deploying a simple asp.net core app that uses only the home controller and a single post method. We have two internal networks one being only Windows machines and the other being only Linux machines. The asp core app has been deployed via NGINX on the linux side. To clarify I can view the website from the Window's side through the Linux Login node's IP Address 10.10.40.50:8081 as the URL. To view the website from the Linux side on the machine hosting the site, it is just localhost:5000.
The post method simply takes a single input and returns a corresponding pdf file to the input, and is as follows:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(string label) {
if (ModelState.IsValid && label.Length > 0) {
string newLabel = label.Trim().Split(' ')[0].ToUpper();
if (fstickChecker.Contains(newLabel )) {
var pdf = await CreatePDF(newLabel);
return pdf;
}
else {
return RedirectToAction("Index", new RouteValueDictionary(new { action = "Index", err = "Invalid label selected" }));
}
}
return View();
}
The razor code:
<form method="post" action="/Home/Create">
@Html.AntiForgeryToken()
<input size="60" type="text" name="label" class="form-control" id="labeler" placeholder="Enter Label" style="float:left;">
<div class="rotator-buttons" style="padding-top:45px;">
<button type="submit" class=" btn btn-01 t-white" style="float:left; height:40px;width:140px;background-color:#706552">Create PDF</button>
</div>
</form>
The post method operates correctly when run on a Windows machine via the VS 2017 debugger and from the linux machine hosting it (i.e. when using the localhost:5000 address).
However it fails when viewing the site from the Windows side (i.e. the 10.10.40:8081).
There is a GET method (named Search) which provides Search Engine type functionality in the textbox where the label is entered that operates correctly under all circumstances so it appears just the POST method is failing for whatever reason.
My program.cs portion:
public class Program {
public static void Main(string[] args) {
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
My startup.cs file
public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
app.UseStaticFiles();
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
}
else {
app.UseExceptionHandler("/Home/Error");
}
//app.UseCookiePolicy();
app.UseMvc(routes => {
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
The NGINX sites-available file (where I think the issue is probably coming from) is:
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
# Default server configuration
#
server {
listen 8081 default_server;
listen [::]:8081 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
#try_files $uri $uri/ =404;
proxy_pass http://localhost:5000/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
#proxy_set_header Connection 'upgrade';
proxy_set_header Connection $http_connection;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /Home/ {
proxy_pass http://localhost:5000/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection;
#proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php7.0-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php7.0-fpm:
# fastcgi_pass unix:/run/php/php7.0-fpm.sock;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
I have found some related stackoverflow questions but none of them were able to fix my problem.
<https://stackoverflow.com/questions/47529210/400-status-error-when-posting-form-data-in-asp-net-core> and
<https://stackoverflow.com/questions/49126850/net-core-nginx-hosting-sockets-doesnt-allow-http-post?noredirect=1&lq=1>
答案1
得分: 0
我最终将“Create”的POST请求更改为GET,问题得以解决。虽然不是理想的解决方案,但在我的情况下,我可以应付。
英文:
I ended up changing the POST request for "Create" to a GET, and that solved the issue. Not ideal, but for my situation I could get away with it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论