3

I have two url

http://localhost/?shop=test

http://localhost/login?shop=test

first url is working. But second url coming 404 nginx page. how can I fix this problem. I want to every location come header if exist shop query

server {
        listen 8081 default_server;
        listen [::]:8081 default_server;

        server_name _;

        location / {
                if ( $arg_shop ) {
                        add_header Content-Security-Policy "frame-ancestors https://$arg_shop";
                }
                root /home;
                index index.html;
                include  /etc/nginx/mime.types;
                try_files $uri $uri/ /index.html?$query_string;
        }
}
2
  • Don't use if inside a location, it doesn't work the way you expect. Try moving the if block one level up into the server block. Commented Apr 6, 2022 at 9:14
  • @RichardSmith can you give answer? I dont undestand completly Commented Apr 6, 2022 at 9:19

2 Answers 2

3

The problem with using if inside a location, is that it doesn't work the way you expect.

You can use a map to define the value of the add_header directive. If the argument is missing or empty, the header will not be added.

For example:

map $arg_shop $csp {
    ""      "";
    default "frame-ancestors https://$arg_shop";
}
server {
    ...

    add_header Content-Security-Policy $csp;

    location / {
        ...
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

it is work like that so healthy. thanks
0

I fixed like that

server {
        listen 8081 default_server;
        listen [::]:8081 default_server;

        server_name _;

        location / {
                error_page 404 = @error_page;

                if ( $arg_shop ) {
                        add_header "Content-Security-Policy" "frame-ancestors https://$arg_shop";
                }

                root /home;
                index index.html;
                include  /etc/nginx/mime.types;
                try_files $uri $uri/ /index.html?$query_string;
        }

        location @error_page {
                add_header "Content-Security-Policy" "frame-ancestors https://$arg_shop";
                root /home;
                index index.html;
                include  /etc/nginx/mime.types;
                try_files $uri $uri/ /index.html?$query_string;
        }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.