ansible-role-nginx-config icon indicating copy to clipboard operation
ansible-role-nginx-config copied to clipboard

how to insert an if statement in the server stanza

Open simevo opened this issue 3 years ago • 3 comments

how would I insert an if statement such as:

if ($request_method = POST) {
    return 405;
}

in the server stanza? Thanks!

simevo avatar Apr 12 '22 04:04 simevo

You would use the custom_directives parameter like it's used in https://github.com/nginxinc/ansible-role-nginx-config/blob/main/molecule/default/converge.yml#L702 but in the server block instead.

alessfg avatar Apr 12 '22 11:04 alessfg

many thanks! based on your tip, I fixed my issue with this code:

- template_file: http/default.conf.j2
  deployment_location: /etc/nginx/sites-available/xxxxxxx
  config:
    servers:
      - core:
          ...
        ssl:
          ...
        locations:
          - location: /
            ...
        custom_directives:
          - if ( $host !~* ^yyyyyy.example.com$ ) { return 444; }

you can close this issue from my part ...

NOTE: the remark here that "You need to add a semi-colon at the end of each directive" is misleading; in this case the strimg must end with }

simevo avatar Apr 12 '22 12:04 simevo

Point taken, I will make a small edit to the docs and close this issue once that is done 😄

alessfg avatar Apr 12 '22 12:04 alessfg