简体   繁体   English

ASP.Net Core 2.2 通过 NGINX 部署在 Linux 上,POST 请求返回 HTTP 错误 400

[英]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.我正在部署一个简单的 asp.net 核心应用程序,它只使用家庭控制器和一个 post 方法。 We have two internal networks one being only Windows machines and the other being only Linux machines.我们有两个内部网络,一个只有 Windows 机器,另一个只有 Linux 机器。 The asp core app has been deployed via NGINX on the linux side. asp 核心应用程序已通过 NGINX 部署在 linux 端。 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.为了澄清起见,我可以通过 Linux 登录节点的 IP 地址 10.10.40.50:8081 作为 URL 从窗口一侧查看网站。 To view the website from the Linux side on the machine hosting the site, it is just localhost:5000.要在托管该站点的机器上的 Linux 端查看该网站,它只是 localhost:5000。

The post method simply takes a single input and returns a corresponding pdf file to the input, and is as follows: post方法只是简单的接受单个输入,并返回一个对应的pdf文件给输入,如下:

[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 (ie when using the localhost:5000 address).当通过 VS 2017 调试器在 Windows 机器上和托管它的 linux 机器上运行时,post 方法可以正确运行(即使用 localhost:5000 地址时)。

However it fails when viewing the site from the Windows side (ie the 10.10.40:8081).但是,从 Windows 端(即 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.有一个 GET 方法(名为 Search),它在输入标签的文本框中提供搜索引擎类型功能,在所有情况下都可以正常运行,因此看起来只是 POST 方法由于某种原因而失败。

My program.cs portion:我的 program.cs 部分:

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我的startup.cs文件

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: NGINX 站点可用文件(我认为问题可能来自于此)是:

##
# 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.我发现了一些相关的 stackoverflow 问题,但没有一个能够解决我的问题。 400 status error when posting form data in ASP.Net Core and .net core nginx hosting sockets doesn't allow http post 在 ASP.Net Core.net core nginx 托管套接字中发布表单数据时出现 400 状态错误不允许 http 发布

I ended up changing the POST request for "Create" to a GET, and that solved the issue.我最终将“创建”的 POST 请求更改为 GET,从而解决了问题。 Not ideal, but for my situation I could get away with it.不理想,但对于我的情况,我可以逃脱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM