【问题标题】:POST request not allowed - 405 Not Allowed - nginx, even with headers included不允许 POST 请求 - 不允许 405 - nginx,即使包含标头
【发布时间】:2014-08-16 09:53:45
【问题描述】:

我在尝试在我的应用程序中执行 POST 请求时遇到问题,我进行了很多搜索,但没有找到解决方案。

所以,我有一个 nodeJS 应用程序和一个网站,我正在尝试使用来自该网站的表单进行 POST 请求,但我总是以这种方式结束:

在控制台中我看到了:

    Uncaught TypeError: Cannot read property 'value' of null 
Post "http://name.github.io/APP-example/file.html " not allowed

就是在这行代码中:

文件.html:

<form id="add_Emails" method ="POST" action="">

    <textarea rows="5" cols="50" name="email">Put the emails here...
    </textarea>

        <p>
        <INPUT type="submit" onclick="sendInvitation()" name='sendInvitationButton' value ='Send Invitation'/>
        </p>


</form>

<script src="scripts/file.js"></script>

文件.js:

function sendInvitation(){

    var teammateEmail= document.getElementById("email").value;

我阅读了许多关于跨域的帖子和文档,但没有成功。 研究来源1:http://enable-cors.org/server.html 研究来源2:http://www.w3.org/TR/2013/CR-cors-20130129/#http-access-control-max-age

我现在在做什么:

我正在尝试从我的服务器的不同域发布:

发布请求:http://name.github.io/APP-example/file.html,github 存储库

POST LISTENER : "http://xxx.xxx.x.xx:9000/email , 服务器 localhost (x-> 我的 ip 地址)

所以,我在其他文件中遇到了同样的问题,但我修复了它,将这段代码放在每条路线的开头:

var express = require('express');
var sha1 = require('sha1'); 

var router = express.Router(); 
var sessionOBJ = require('./session');

var teams = {} 
var teamPlayers = []

router.all('*', function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  res.header("Access-Control-Allow-Methods", "PUT, GET,POST");
  next();
 });

我解决了它。

现在,我遇到了同样的问题,但在这个文件中,唯一的区别是我处理 SMTP 和电子邮件,因此我发布了一封电子邮件并将电子邮件发送到我在 POST 请求中收到的这封电子邮件。

该代码在 POSTMAN 中运行良好,因此,当我使用 POSTMAN 进行测试时,它可以正常工作并且我可以发布。

我在下面添加了这段代码,而不是我展示的第一个代码,但它也不起作用:

router.all('*', function(req, res, next){
            res.header("Access-Control-Allow-Origin", "*")
            res.header("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
            res.header("Access-Control-Allow-Headers", "Origin, Content-Type, Accept")
            res.header("Access-Control-Max-Age", "1728000")
            next();
        });

有人知道如何解决吗?

谢谢。

【问题讨论】:

  • 我认为问题的出现是因为nginx does not allow POST to static content
  • 静态内容怎么来的?你能更清楚一点吗?谢谢你的评论! :)
  • 如果nginx的某个location包含proxy_passfastcgi_pass指令,这是动态内容,否则--静态。换句话说,静态内容是 nginx 简单地从文件系统读取文件并按原样发送的情况。动态内容是当某种编程语言产生响应时。要解决这个问题,你应该确保处理http://xxx.xxx.x.xx:9000/email请求的nginx的location包含proxy_pass指令
  • 这篇文章介绍了解决静态文件中 405 Nginx 状态码的 3 种方法:405 not allowed Nginx fix for POST requests

标签: node.js express nginx cross-domain nodemailer


【解决方案1】:

这个配置到你的 nginx.conf 应该对你有帮助。

https://gist.github.com/baskaran-md/e46cc25ccfac83f153bb

server {
    listen       80;
    server_name  localhost;

    location / {
        root   html;
        index  index.html index.htm;
    }

    error_page  404     /404.html;
    error_page  403     /403.html;

    # To allow POST on static pages
    error_page  405     =200 $uri;

    # ...
}

【讨论】:

  • 这是一个hack,你也懒得解释。
  • 看来他只是将 405 改写为 200 - 让它看起来有效。很高兴知道这是否有效。即,它会返回静态资源/
  • 哇,它有效。但它和肮脏的黑客真的没有区别。
  • 我正在使用 wordpress 插件将数据发布到不同的服务器,然后返回响应。有时它工作正常,但在大约 95% 的情况下它失败并返回“405 method not allowed nginx”。我必须在哪里做这个 nginx.conf 设置?我必须在远程服务器上安装 nginx 并进行此配置吗?请帮助我面临几乎类似的问题,但方式略有不同。
【解决方案2】:

这是到预期服务器的真正代理重定向。

server {
  listen          80;
  server_name     localhost;
location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
    proxy_pass http://xx.xxx.xxx.xxx/;
    proxy_redirect off;
    proxy_set_header Host $host;

  }
}

【讨论】:

    【解决方案3】:

    我注意到这不适用于先静态后反向代理设置。看起来是这样的:

    location @app {
      proxy_pass http://localhost:3000$request_uri;
    }
    
    location / {
      try_files $uri $uri/ @app;
      error_page 405 @app;
    }
    

    【讨论】:

      【解决方案4】:

      我已经尝试了将 405 重定向到 200 的解决方案,并且在生产环境中(在我的情况下,它是带有 Nginx Docker 容器的 Google 负载平衡),此 hack 导致一些 502 错误(Google 负载平衡错误代码:backend_early_response_with_non_error_status)。

      最后,我将 Nginx 替换为与 Nginx 完全兼容并具有更多插件的 OpenResty,从而使其正常工作。

      使用ngx_coolkit,现在 Nginx(OpenResty) 可以正确地为静态文件提供 POST 请求,这里是我的配置文件:

      server {
        listen 80;
      
        location / {
          override_method GET;
          proxy_pass http://127.0.0.1:8080;
        }
      }
      
      server {
        listen 8080;
        location / {
          root /var/www/web-static;
          index index.html;
          add_header Cache-Control no-cache;
        }
      }
      

      在上述配置中,我使用ngx_coolkit 提供的override_method 将HTTP 方法覆盖为GET

      【讨论】:

        【解决方案5】:

        我遇到了类似的问题,但只有 Chrome 才能正常工作。我注意到 Chrome 在标头请求中添加了一个 Origin 参数。

        所以在我的 nginx.conf 中,我在 location/ 块下添加了参数以避免它

        proxy_set_header Origin "";
        

        【讨论】:

          【解决方案6】:

          在我的情况下,它是 POST 提交的要处理的 json 并获得返回值。我交叉检查了使用和不使用 nginx 的应用服务器的日志。我得到的是我的位置没有附加到 proxy_pass url 并且 HTTP 协议版本的版本不同。

          • 没有 nginx:“POST /xxQuery HTTP/1.1”200 -
          • 使用 nginx:“POST / HTTP/1.0” 405 -

          我之前的位置块是

          location /xxQuery {
              proxy_method POST;
              proxy_pass http://127.0.0.1:xx00/;
              client_max_body_size 10M;
          }
          

          我改成

          location /xxQuery {
              proxy_method POST;
              proxy_http_version 1.1;
              proxy_pass http://127.0.0.1:xx00/xxQuery;
              client_max_body_size 10M;
          }
          

          成功了。

          【讨论】:

            【解决方案7】:

            我在使用 VueJs 作为托管在 example.com 的前端应用程序时遇到了类似的问题,而我的 Rails 应用程序作为托管在 api.example.com 的 API。 Vue 被配置为通过ENV 变量VUE_APP_API_BASE_URL 获取数据。

            我犯了一个新手错误,忘记在生产服务器上创建env.production.local,因此在构建 VueJs 应用程序时,该 URL 从未被拾取。因此,所有请求都被路由到例如example.com/signin 而不是api.example.com/signin,因此会出现错误。

            在生产服务器上创建文件并添加变量,然后重新构建 vuejs 应用程序一切正常!

            【讨论】:

              【解决方案8】:

              为时已晚,如果有人遇到这个问题,请在 nginx.conf 中添加以下代码

              server {
              
                      ...
              
                      # pass PHP scripts to FastCGI server
                      #
                      location ~ \.php$ {
                              include snippets/fastcgi-php.conf;
              
                              # With php-fpm (or other unix sockets):
                              fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
                              # With php-cgi (or other tcp sockets):
              
                      }
              
              
              }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2015-09-08
                • 2016-10-03
                • 2014-05-23
                • 2014-06-22
                • 2023-03-10
                相关资源
                最近更新 更多