Skip to content

Location Snippets ignored when using Action.Return #5746

@jasonwilliams14

Description

@jasonwilliams14

Discussed in #5733

Originally posted by privateVoit June 12, 2024
I want to expose a robots.txt endpoint. This can be done quiet easily with the Action.Return. Additionally I want to add CORS Headers. As there doesn't seem to be an option for this I am using location-snippets. The issue is that the location snippet is ignored. Heres the config:

apiVersion: k8s.nginx.org/v1
kind: VirtualServer
metadata:
  name: example.com
  namespace: default
spec:
  host: example.com
  routes:
  - path: /robots.txt
    action:
      return:
        body: |-
          User-agent: *
          Disallow: /
        code: 200
        type: text/plain
    location-snippets: |
      add_header 'Cache-Control' 'private, max-age=0' always;
      add_header 'Cross-Origin-Resource-Policy' 'cross-origin' always;
      add_header 'X-Content-Type-Options' 'nosniff' always;
      add_header 'X-Xss-Protection' '1; mode=block' always;
      add_header 'X-Frame-Options' 'SAMEORIGIN' always;
      add_header 'Frame-Options' 'SAMEORIGIN' always;

The result using curl is:

> GET /robots.txt HTTP/2
> Host: example.com
> User-Agent: curl/8.6.0
> Accept: */*
> 
< HTTP/2 200 
< server: nginx
< date: Wed, 12 Jun 2024 07:46:11 GMT
< content-type: text/plain
< content-length: 25
< 
User-agent: *
* Connection #0 to host example.com left intact
Disallow: /%  

It's simply missing within the rendered nginx.conf:

    location @return_0 {
        default_type "text/plain";
        # status code is ignored here, using 0
        return 0 "User-agent: *
Disallow: /";
    }
    
    location /robots.txt {
        set $service "";
        status_zone "";

        error_page 418 =200 "@return_0";
        proxy_intercept_errors on;
        proxy_pass http://unix:/var/lib/nginx/nginx-418-server.sock;
        set $default_connection_header close;
    }

I did enable the location-snippets flag and it works when using the proxy option:

apiVersion: k8s.nginx.org/v1
kind: VirtualServer
metadata:
  name: example.com
  namespace: default
spec:
  host: example.com
  routes:
    - action:
        proxy:
          upstream: nginx-app
      location-snippets: |
        add_header 'Cache-Control' 'private, max-age=0' always;
        add_header 'Cross-Origin-Resource-Policy' 'cross-origin' always;
        add_header 'X-Content-Type-Options' 'nosniff' always;
        add_header 'X-Xss-Protection' '1; mode=block' always;
        add_header 'X-Frame-Options' 'SAMEORIGIN' always;
        add_header 'Frame-Options' 'SAMEORIGIN' always;
      path: /
  upstreams:
    - name: nginx-app
      port: 8080
      service: nginx-app

The location snippet is added to the nginx.conf

    location / {
        set $service "nginx-app";
        status_zone "nginx-app";
        set $resource_type "virtualserver";
        set $resource_name "example.com";
        set $resource_namespace "default";
        add_header 'Cache-Control' 'private, max-age=0' always;
        add_header 'Cross-Origin-Resource-Policy' 'cross-origin' always;
        add_header 'X-Content-Type-Options' 'nosniff' always;
        add_header 'X-Xss-Protection' '1; mode=block' always;
        add_header 'X-Frame-Options' 'SAMEORIGIN' always;
        add_header 'Frame-Options' 'SAMEORIGIN' always;
        
...

I couldn't find any corresponding log entries to this. And I tried version 3.1 and 3.5 and they both do not work.

Can anybody help?

Metadata

Metadata

Labels

backlogPull requests/issues that are backlog itemsbugAn issue reporting a potential bugwaiting for responseWaiting for author's response

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions