How to configure the Nginx to support the HTTP POST method

How to configure the Nginx to support the HTTP POST method

The docker is more useful to test and debug the code to communicate with the Web server rather than the real web server. The following post says that how to run the web server container with the docker.

The Docker is useful to test and to debug the code to connect with HTTP/HTTPS

However, the docker can’t handle the HTTP method with the default configuration. This post explains that the Nginx in the container returns the contents of the file at URL same as GET method.

Error Response

The Nginx with the default configuration returns the response below when the client program connects with the HTTP POST method.

<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>

Configure the Nginx

Edit following two files to configure the Nginx.

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

Copy default configuration files

To edit configuration files, copy default files from the docker container to the local. Execute following commands in the terminal.

$ 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

The container name is testserver_nginx_1 in my machine, but this may be not same in your machine. You need to change it to your environment. Above two commands copy the nginx.conf and default.conf from the container.

What is “docker container cp” command

The “docker container cp” command copy the file in the container to the local file system. The format is below.

# Docker Container -> Local File System
docker container cp CONTAINER:path_to_file_in_container path_to_file_in_local

# Local File System -> Docker Container
docker container cp path_to_file_in_local CONTAINER:path_to_file_in_container

Configure to handle POST Method

You can find following configuration in nginx.conf, the HTTP configuration is load from the file in /etc/nginx/conf.d directory.

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

The server configuration is found in default.conf file. Edit this file to make the Nginx to handle POST method.

server {
	error_page 405 =200 $uri;
}

The configuration file is now to be below.

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;
    }

}

Configure the Docker

Dockerfile

Add following configuration to copy the new configuration file into the container from the local file system.

FROM nginx:1.17

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

What is COPY

The COPY command copy the file from the local file system into the container on building the container.

Rebuild the container

Rebuild the container with the docker image. Use Docker Compose.

$ docker-compose build

Test

Run the container with the docker-compose.

$ docker-compose up -d

Connect the container with the HTTP method from your application.

Sponsored Links
Get the Latest News !

Developer ToolsLatest 8 Posts