<p><em>I know this is an oldish question, but I hit a similar issue, so leaving this here for posterity...</em></p>
<p>Per the <a href="https://ddev.readthedocs.io/en/latest/users/extend/customization-extendibility/#custom-nginx-configuration" rel="nofollow noreferrer">ddev docs</a> you can edit the nginx conf file at <code>.ddev/nginx_full/nginx-site.conf</code>.</p>
<p>First of all, you need to remove the <code>#ddev-generated</code> comment line near the top so that ddev doesn't overwrite your changes, and you will probably also want to <code>git add -f .ddev/nginx_full/nginx-site.conf</code> once you've customised it so that you can share your changes with others / persist them over time.</p>
<p>You can then edit the nginx config, run <code>ddev exec nginx -t</code> to test your config is valid and if so finally run <code>ddev restart</code> to see your changes. For your specific use-case, just drop your <code>location {}</code> blocks inside the <code>server {}</code> block in <code>.ddev/nginx_full/nginx-site.conf</code> and  <code>ddev restart</code>.</p>

CC BY-SA 4.0
2023-09-13T11:43:03.260Z
Oh thanks. That's great. I will try the next time I run into this issue. I know, that I was not able to solve it, when I asked this question...
Merc
CC BY-SA 4.0
2023-10-22T20:00:47.357Z
Worked for me, thanks for the clear instructions
kmgdev
CC BY-SA 4.0
2023-09-13T11:13:12.263Z
2023-09-13T11:13:12.263Z
Tom Davies
<p>For some reason adding the header inside a <code>location</code> clause didn't work for me. This is a very dirty development-only solution but I just added <code>add_header Access-Control-Allow-Origin *;</code> to the end of the <code>nginx-site.conf</code> file, just before the closing bracket of the <code>server</code> clause. Then ran <code>ddev restart</code> and FINALLY got a proper CORS response header.</p>

CC BY-SA 4.0
2024-05-04T21:16:45.750Z
2024-05-04T21:16:45.750Z
kmgdev
<p>I have Craft 4 installed and use it locally with ddev.
Ddev uses nginx in my case and I want to enable serving .json files from a subdirectory within <code>/web</code>.</p>
<p>What do I have to add to the nginx.conf file?</p>
<p>I tried:</p>
<pre><code># json
location ~* \.(?:json?)$ {
    add_header Access-Control-Allow-Origin &quot;*&quot;;
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept';
}

location /data/data-charging-stations/ {
    try_files $uri /index.php?$query_string;
    add_header Access-Control-Allow-Origin &quot;*&quot;;
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept';
    expires    7d;
    access_log off;
}
</code></pre>
<p>(Whereas my json is within <code>/web/data/data-charging-stations</code>).</p>
<p>my ddev config yaml is defined to use <code>webserver_type: nginx-fpm</code></p>
<p>And I found an nginx config here:</p>
<p><code>/.nginx-config/nginx.conf</code></p>
<p>I don't know if that config is even active. using <code>curl</code> to test the json never shows the added headers.</p>
<p>I would be very glad if somebody could help me out here.</p>
<p>Thanks</p>

<nginx><ddev><cors>
Nginx Config to serve .json without CORS restriction with ddev
# Nginx Config to serve .json without CORS restriction with ddev

(Asked by: username_0 on 2023-03-30)

I have Craft 4 installed and use it locally with ddev.
Ddev uses nginx in my case and I want to enable serving .json files from a subdirectory within /web.
What do I have to add to the nginx.conf file?
I tried:
# json
location ~* \.(?:json?)$ {
    add_header Access-Control-Allow-Origin "*";
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept';
}

location /data/data-charging-stations/ {
    try_files $uri /index.php?$query_string;
    add_header Access-Control-Allow-Origin "*";
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept';
    expires    7d;
    access_log off;
}

(Whereas my json is within /web/data/data-charging-stations).
my ddev config yaml is defined to use webserver_type: nginx-fpm
And I found an nginx config here:
/.nginx-config/nginx.conf
I don't know if that config is even active. using curl to test the json never shows the added headers.
I would be very glad if somebody could help me out here.
Thanks

**Tags:** nginx, ddev, cors


---

## 2 answers

### Answer by username_1 on 2023-09-13. Score: 2

*I know this is an oldish question, but I hit a similar issue, so leaving this here for posterity...*

Per the [ddev docs](https://ddev.readthedocs.io/en/latest/users/extend/customization-extendibility/#custom-nginx-configuration) you can edit the nginx conf file at `.ddev/nginx_full/nginx-site.conf`.

First of all, you need to remove the `#ddev-generated` comment line near the top so that ddev doesn't overwrite your changes, and you will probably also want to `git add -f .ddev/nginx_full/nginx-site.conf` once you've customised it so that you can share your changes with others / persist them over time.

You can then edit the nginx config, run `ddev exec nginx -t` to test your config is valid and if so finally run `ddev restart` to see your changes. For your specific use-case, just drop your `location {}` blocks inside the `server {}` block in `.ddev/nginx_full/nginx-site.conf` and `ddev restart`.

> **Comments:**
> **username_0**: Oh thanks. That's great. I will try the next time I run into this issue. I know, that I was not able to solve it, when I asked this question...  
> **username_2**: Worked for me, thanks for the clear instructions  

---

### Answer by username_2 on 2024-05-04. Score: 1

For some reason adding the header inside a `location` clause didn't work for me. This is a very dirty development-only solution but I just added `add_header Access-Control-Allow-Origin *;` to the end of the `nginx-site.conf` file, just before the closing bracket of the `server` clause. Then ran `ddev restart` and FINALLY got a proper CORS response header.


---

