大网数据广告活动
服务器运维问答

Nginx POST request is changed to GET when using Ajax

· 52CE 我爱测

I have an Nginx server used for a store. That store has an API which is in a different folder for which i'm using an alias. Here is my example.com from sites-enabled in Nginx: server { listen 443 ssl; listen [::]:443 ssl; server_…

问题原文

原帖链接 · emma · CC BY-SA 4.0

I have an Nginx server used for a store. That store has an API which is in a different folder for which i'm using an alias. Here is my example.com from sites-enabled in Nginx:

server {
    listen 443 ssl;
    listen [::]:443 ssl;

    server_name www.example.com;

    root /var/www/store/public;

    #certificated go here [edited]

    return 301 https://example.com$request_uri;
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;

    server_name example.com;

    root /var/www/store/public;

    index index.php;

    #certificates go here [edited]

    location /admin {
            index index.php;
    }

    location /api {
            alias /var/www/api/public;
            
            try_files $uri $uri/ @api;

            location ~ \.php$ {
                    include snippets/fastcgi-php.conf;
                    fastcgi_param SCRIPT_FILENAME $request_filename;
                    fastcgi_split_path_info ^(.+\.php)(/.+)$;
                    fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
            }
    }

    location @api{
            rewrite /api/(.*)$ /api/index.php?/$1 last;
    }
  
    #the rest of the store config block [edited]

}

The problem that i have is that if i send a POST request using ajax to 'example.com/api' it is changed to a GET and all the parameters are lost.

Here is my Ajax code:

var foo = 'foo';
var bar = 'bar';

$.ajax({
    type: 'POST',

    beforeSend: function(request) {
        request.setRequestHeader("Authorization", 'xyz');
    },
    url: "https://example.com/api",
    data: { foo: foo, bar: bar},
    processData: false,
    success: function(data) {
        console.log(data);
    }
});

To test this i simply dumped $_SERVER['REQUEST_METHOD'].

If i call for example.com/api/index.php the request type is as it should be POST but if i call just example.com/api it is changed to GET.

How can i preserve the request type and parameters for a request?

已采纳解答

原帖链接 · Michael Hampton · CC BY-SA 4.0

Your use of a location @api and a rewrite doesn't make much sense here. As it stands, your requests have to get processed through three different location blocks before they are finally sent upstream to PHP.. It is also reversed from the normal pattern I would expect to see.

It looks like the request is getting mangled by the automated 301 redirect from /api to /api/ that web servers always apply to paths that correspond to a directory, as Ivan Shatsky mentioned in a comment.

I would rewrite these location blocks as:

# This doesn't even need a location
rewrite /api(/.*)? /api/index.php?$1 last;

location /api/index.php {
    alias /var/www/api/public;

    try_files $uri =404;

    include snippets/fastcgi-php.conf;
    fastcgi_param SCRIPT_FILENAME $request_filename;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}

This should also allow you to use /api as a path without the trailing slash, though as Ivan Shatsky also pointed out, this is not a good practice to get into.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-07-09)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。