Nginx POST settings

Accessing Nginx via POST results in an HTTP STATUS 405 error by default, which is a problem when accessing Nginx containerized in Docker from an app under development that returns a fixed XML for debugging purposes. I don’t want this to happen.

This article shows how to make it possible to retrieve files placed in Nginx as they are without error, just like GET.

For more information on how to containerize and run Nginx with Docker, please see the following article.

TOC

Error message

If you send a POST to Nginx with the default settings, you will get the following response.

<html>
<head><title>405 Not Allowed</title></head>
<body>
<center><h1>405 Not Allowed</h1></center>
<hr><center>nginx/1.17.8</center>
</body>
</html>

Nginx configuration

The Nginx configuration is changed by editing the following two files.

  • /etc/nginx/nginx.conf
  • /etc/nginx/conf.d/default.conf

Copy default configuration files

To edit, copy the default configurations file from the Docker container. In a terminal, do the following.

% docker container cp testserver_nginx_1:/etc/nginx/nginx.conf ./nginx.conf
% docker container cp testserver_nginx_1:/etc/nginx/conf.d/default.conf ./default.conf

testserver_nginx_1 is the Docker container name in my environment. Please change it according to your environment. When executed, the nginx.conf and default.conf files will be copied from the container to the current directory.

About the “docker container cp” command

The docker container cp command copies files and folders between a Docker container and the local file system. It uses the following format.

# Docker Container -> Local File System
docker container cp CONTAINER:path-in-container destination-path-in-local

# Local File System -> Docker Container
docker container cp path-in-local CONTAINER:desination-path-in-container

Configure “HTTP STATUS 405”

Looking at the nginx.conf file, we find the following configuration, which indicates that the http configuration is read from a file placed in the /etc/nginx/conf.d directory. In other words, default.conf is read.

http {
    (omission)

    include /etc/nginx/conf.d/*.conf;
}

Let’s look at the other file we copied, default.conf, where we can find the server configuration. For Nginx, the configuration of the HTTP STATUS should be write in the server. If you write in the location, you can apply the setting to only specified directory. In my case, the setting can be applied to whole, so I wrote it in the server.

server {
    (omission)

    error_page 405 =200 $uri;
}

Change HTTP STATUS to 200 and return the accessed file as is.

The entire configuration file is as follows Some comments have been removed.

server {
    listen       80;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    error_page 405 =200 $uri;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

}

Docker Image Configuration

Modify the Docker image configuration so that the settings are reflected.

Edit the “Dockerfile”

Add the following settings to the Dockerfile so that the edited files are copied to the Docker container.

FROM nginx:1.17

COPY default.conf /etc/nginx/conf.d

COPY

COPY allows you to copy files from the local file system to the container when building a Docker image.

COPY source-path dest-path

Rebuild Docker image

Rebuild the Docker image, using Docker Compose, as follows.

% docker-compose build

Test

Run the container as follows.

% docker-compose up -d

Connect from the application via POST, and if the file is received without error, it is successful.

Let's share this post !

Author of this article

Akira Hayashiのアバター Akira Hayashi Representative, Software Engineer

I am an application developer loves programming. This blog is a tech blog, its articles are learning notes. In my work, I mainly focus on desktop and mobile application development, but I also write technical books and teach seminars. The websites of my work and books are here -> RK Kaihatsu.

TOC