Nginx POST settings

By default, accessing Nginx via POST results in an HTTP STATUS 405 error. This presents a problem when accessing an Nginx instance containerized in Docker from an app under development, especially when the app returns a fixed XML for debugging purposes. Avoiding this issue is crucial.

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

Please see the following article for more information on containerizing and running Nginx with Docker.

目次

Error message

The following response will be received when sending a POST to Nginx with the default settings.

<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. Then, 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

In this example, testserver_nginx_1 is the Docker container name used. Adapt this to fit the container name in your specific environment. Upon execution, 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”

We are 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. The HTTP STATUS configuration should be written in the server for Nginx. For example, if you write in the location, you can apply the setting to only the specified directory. In my case, the setting can be applied to the 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 the Docker image

Rebuild the Docker image using Docker Compose, as follows.

% docker-compose build

Test

Run the container as follows.

% docker-compose up -d

Establish a connection from the application via POST. The operation is considered successful if the file is received without any errors.

著書紹介

Authored Books

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

Akira Hayashi (林 晃)のアバター Akira Hayashi (林 晃) Representative(代表), Software Engineer(ソフトウェアエンジニア)

アールケー開発代表。Appleプラットフォーム向けの開発を専門としているソフトウェアエンジニア。ソフトウェアの受託開発、技術書執筆、技術指導・セミナー講師。note, Medium, LinkedIn
-
Representative of RK Kaihatsu. Software Engineer Specializing in Development for the Apple Platform. Specializing in contract software development, technical writing, and serving as a tech workshop lecturer. note, Medium, LinkedIn

目次