Skip to content

Get rid of if usage in nginx config #109

@chiel

Description

@chiel

We tried to get rid of the newly-introduced if statement inside the cors.conf in order to avoid if, since it is generally discouraged by nginx.

With the help of @stevenjm we came to the following:

# /etc/nginx/location.d-enabled/cors.conf
# Allow CORS if environment variable is set
add_header 'Access-Control-Allow-Origin' "http://127.0.0.1:8888" always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Access-Control-Allow-Methods' "*" always;
add_header 'Access-Control-Allow-Headers' "*" always;
add_header 'Access-Control-Expose-Headers' "*" always;
# /etc/nginx/conf.d/default.conf
map $request_method $rewrite_target {
    default /index.php;
    OPTIONS /NGINX_INTERNAL_HANDLE_OPTIONS;
}

server {
    listen 80;
    listen [::]:80;
    server_name  localhost;

    root    /opt/project/public;
    charset UTF-8;

    include /etc/nginx/location.d-enabled/*.conf;

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    
    location / {
        rewrite ^ $rewrite_target last;
    }

    location = /NGINX_INTERNAL_HANDLE_OPTIONS {
        return 204;
    }

    location ~ \.php$ {
        fastcgi_pass   unix:/var/run/php-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;

        include        fastcgi_params;

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $document_root;
    }
}

The map could be moved into the cors.conf and that file could, for example, live in the conf.d directory, but I'll leave that to you guys to figure out.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions