Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get rid of if usage in nginx config #109

Open
chiel opened this issue Apr 30, 2019 · 0 comments
Open

Get rid of if usage in nginx config #109

chiel opened this issue Apr 30, 2019 · 0 comments

Comments

@chiel
Copy link

chiel commented Apr 30, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant