0

I am having an Nginx server, with uwsgi and Wordpress installed on it. So the problem is that whenever I am trying to send a POST request to uwsgi application it fails with the error 405, but the method is allowed on server so I don't know why it happens. Here is the nginx.conf file:

server {
    listen 80;
    server_name hrspot.me;
    return 301 https://hrspot.me$request_uri;
}

server {
    listen 443 ssl;
    server_name hrspot.me;
    index index.php index.html index.htm;
    root /var/www/html;
    ssl_certificate /etc/ssl/bundle.crt;
    ssl_certificate_key /etc/ssl/www.hrspot.me.key;
    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    client_max_body_size 1024m;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass wordpress:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    location ~ /\.ht {
        deny all;
    }

    location = /favicon.ico {
        log_not_found off;
    }

    location = /robots.txt {
        log_not_found off;
    }

    location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
        expires max;
        log_not_found off;
    }

    location /api {
        include uwsgi_params;
        uwsgi_pass hrspotme_server:8080;
    }

}

And here is the method in flask app:

...
@account.route('/api/register', methods=['POST'])
def route_api_register():
    form = RegisterForm()
    return api_register(form)
...

As you can see that is the POST method. For some reason UWSGI perceives it as a GET request And here is the log for this method from UWSGI:

[pid: 10|app: 0|req: 2/3] ip_address () {44 vars in 686 bytes} [Fri Jul 10 13:01:55 2020] GET /api/register => generated 178 bytes in 17 msecs (HTTP/1.1 405) 3 headers in 118 bytes (1 switches on core 1)

So I understand that some kind of problem with the settings.

HOW I MAKE A REQUEST: request

LOGS WHEN I TRY TO SEND POST REQUEST TO /api/auth:

ip - - [10/Jul/2020:13:51:06 +0000] "POST /api/auth HTTP/1.1" 301 169 "-" "PostmanRuntime/7.26.1" "-"
ip - - [10/Jul/2020:13:51:06 +0000] "GET /api/auth HTTP/1.1" 405 178 "http://hrspot.me/api/auth" "PostmanRuntime/7.26.1"

UPD:
I tried to specify https in front of the url in the postman and it worked, so it seems an error that when I initially call on the http request, it incorrectly redirects it to the https request, changing the method from POST to GET. request

GoshkaLP
  • 137
  • 1
  • 11
  • 1
    first you should check what `error 405` means and then you would know what is problem. Log shows you try to run `GET` request (not `POST`) but you defined only `methods=['POST'])` for this function. You need `methods=['GET', 'POST'])`. Are you sure you send POST request? – furas Jul 10 '20 at 13:19
  • @furas but I don't need GET rmethod in this case. The problem is when I am sending a POST request to `/api/register` via Postman, the Nginx regards it like a GET request – GoshkaLP Jul 10 '20 at 13:21
  • 1
    but log show `GET /api/register` , not `POST /api/register` - so you send `GET`, not `POST`. – furas Jul 10 '20 at 13:22
  • @furas look at the screenshot, it is clearly visible that I am sending a post request - `https://imgur.com/a/e8HEsB0` – GoshkaLP Jul 10 '20 at 13:25
  • you could add this image in question - more people will see it. And maybe someone will have better idea. – furas Jul 10 '20 at 13:27
  • @furas added it – GoshkaLP Jul 10 '20 at 13:28
  • at this moment I have no idea. And at this moment I don't have configured nginx with uwsgi and flask to test it. But first I would check nginx logs. – furas Jul 10 '20 at 13:44
  • @furas So i tried to delete wordpress and run nginx only with UWSGI, but I got the same error :( – GoshkaLP Jul 10 '20 at 13:56
  • what do you have in `/etc/nginx/uwsgi_pass` which is used by `include uwsgi_params;` ? Maybe there are settings which change `POST` to `GET`? BTW: what if you set `methods=['GET', 'POST']` - is it running without other problems? Maybe this gives some new information. – furas Jul 10 '20 at 14:10
  • @furas I tried to specify https in front of the url in the postman and it worked, so it seems an error that when I initially call on the http request, it incorrectly redirects it to the https request, changing the method from POST to GET – GoshkaLP Jul 10 '20 at 14:52
  • https://i.imgur.com/mBNbuu0.png – GoshkaLP Jul 10 '20 at 14:52
  • 1
    [Nginx loses POST variable with http -> https redirect](https://stackoverflow.com/questions/39280361/nginx-loses-post-variable-with-http-https-redirect), [Redirecting HTTP POST requests to HTTPS POST requests](https://stackoverflow.com/questions/31620987/redirecting-http-post-requests-to-https-post-requests), etc. – furas Jul 10 '20 at 20:42
  • @furas thx a lot! – GoshkaLP Jul 10 '20 at 21:01

0 Answers0