Note: nginx.conf settings for CORS in Exment on Lightsail + Docker (nginx) environment

The other day, I wrote The story of building an OSS WebDB system called Exment with the combination of Lightsail + Docker.

After that, I also wrote Article that combines Exment and MF cloud invoice with API.

So, you forgot to write important things in both articles.

Exment API doesn't work

It works with Postman, but it doesn't work with Node.js. Why ... Yes, CORS ...

So, Docker-compose that operates Extension is here, but the web server is running on nginx.

That's why I added the CORS settings for nginx.

nginx.conf


server {
    listen 8080;
    server_name _;

    root  /var/www/exment/public;
    index index.php index.html;

    access_log /var/log/nginx/access.log;
    error_log  /var/log/nginx/error.log;

    location / {
        if ($request_method = 'OPTIONS') {
            add_header Access-Control-Allow-Origin '*';
            add_header Access-Control-Allow-Methods 'GET, POST, PUT, DELETE';
            add_header Access-Control-Allow-Headers 'Origin, Authorization, Accept, Content-Type';
            add_header Access-Control-Max-Age 3600;

            add_header Content-Type 'text/plain charset=UTF-8';
            add_header Content-Length 0;

            return 204;
         }

        try_files $uri /index.php?$query_string;
    }

    location ~ \.php$ {
        add_header Access-Control-Allow-Origin '*' always;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include       fastcgi_params;
    }
}

--Reference: CORS setting definitive edition for setting up API server

Recommended Posts

Note: nginx.conf settings for CORS in Exment on Lightsail + Docker (nginx) environment
Configuration script for using docker in proxy environment on ubuntu 20.04.1
[Docker] Use environment variables in Nginx conf
Just install Laravel8 on docker in PHP8 environment
Points stuck when running vite + Nginx in Docker environment
[Note] Build a Python3 environment with Docker in EC2
[Rails] Settings for loading web fonts in CloudFront on Heroku
Build Metabase with Docker on Lightsail and make it https with nginx
Build a development environment for Django + MySQL + nginx with Docker Compose