<p>IMO, the issue is with this line.</p>

<pre><code>SetEnv HTTP_ACCEPT text/html,application/xhtml+xml,application/xml,application/json;q=0.9,*/*;q=0.8
</code></pre>

<p>With ACCEPT headers set to a fixed value, it's impossible for Craft 3 to distinguish between Json and HTML requests. This must have been a necessity with the nginx proxy setup.</p>

<p>I suggest removing this line from Apache and configure nginx to pass through the original request headers.</p>

<p>If you use proxy_pass, trying adding this line</p>

<pre><code>location / {
    ...
    proxy_pass_request_headers on;
    ...
}
</code></pre>

<p>OR, if you prefer a more direct approach</p>

<pre><code>location / {
    ...
    proxy_set_header Accept $http_accept;
    ...
}
</code></pre>

CC BY-SA 4.0
2018-05-09T02:00:46.543Z
2018-05-09T02:00:46.543Z
Hieu Nguyen
<p>Craft Version: 3.0.4<br>
Web Server: Apache + Nginx proxy </p>

<p>Recently I've been working on a couple of Craft 3 projects for deployment on my institution's web servers. In the past, I've successfully launched numerous Craft 2 projects without server-specific issues, save for needing to set the Apache <code>HTTP_ACCEPT</code> header as follows in .htaccess (because it wasn't already configured by httpd.conf):</p>

<p><code>SetEnv HTTP_ACCEPT text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
</code></p>

<p>On my initial attempts to deploy Craft 3 to the same servers (and using the same <code>HTTP_ACCEPT</code> configuration in .htaccess), I ran into odd behavior when trying to log in: initially the login process would fail silently (i.e. submitting username/password would process but remain on the login page), but if I were to then immediately refresh the login page a logged-in session would be established and Craft would load the CP.</p>

<p>Once the CP loaded, Craft would throw an error ("An unknown error occurred") and subsequent attempts to navigate through the CP would throw the same error and content such as lists of entries would not appear. Further investigation of network request showed that <code>/actions</code> (POST, GET) requests were failing with <code>400 Bad Request</code>, with a response of <code>"Request must accept JSON in response"</code>.</p>

<p>Initially, before I happened upon actual bad request response details, the folks at Craft thought the issue might pertain to an inconsistent use of HTTPS, or something specific to the use of the Nginx proxy server.</p>

<p>As a test, however, I added <code>application/json</code> to the <code>HTTP_ACCEPT</code> header as follows:</p>

<p><code>SetEnv HTTP_ACCEPT text/html,application/xhtml+xml,application/xml,application/json;q=0.9,*/*;q=0.8</code></p>

<p>Low and behold, I was then able to successfully log in and navigate the CP. All of the POST and GET requests to <code>/actions</code> responded successfully.</p>

<p>Now, however, I've ended up in spot where saving an entry successfully commits the change to the database but renders the raw JSON response of the POST request on the page rather than returning to the entry's edit page. Here's an example of what I see in the browser when saving an entry:</p>

<p><code>{"success":true,"id":15107,"title":"test","slug":"test","cpEditUrl":"https://my.domain.com/operations/entries/generalPages/15107-test","authorUsername":"admin","dateCreated":"2018-05-02T10:08:10-05:00","dateUpdated":"2018-05-02T10:08:10-05:00","postDate":null}
</code></p>

<p>The url in the browser remains on the entry such that if I refresh the page, I'm returned to the entry in the CP and see the updated content.</p>

<p>At this point, I'm stuck and not sure if the <code>HTTP_ACCEPT</code> header is configured incorrectly, or if the Nginx proxy server is indeed interfering with the requests.</p>

<p>Many thanks in advance for thoughts/assistance.</p>

<craft3><server><json><nginx><apache>
When saving an Entry, I receive a JSON response rather than returning to the Entry's edit page
# When saving an Entry, I receive a JSON response rather than returning to the Entry's edit page

(Asked by: username_0 on 2018-05-02)

Craft Version: 3.0.4  
Web Server: Apache + Nginx proxy

Recently I've been working on a couple of Craft 3 projects for deployment on my institution's web servers. In the past, I've successfully launched numerous Craft 2 projects without server-specific issues, save for needing to set the Apache `HTTP_ACCEPT` header as follows in .htaccess (because it wasn't already configured by httpd.conf):

`SetEnv HTTP_ACCEPT text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8`

On my initial attempts to deploy Craft 3 to the same servers (and using the same `HTTP_ACCEPT` configuration in .htaccess), I ran into odd behavior when trying to log in: initially the login process would fail silently (i.e. submitting username/password would process but remain on the login page), but if I were to then immediately refresh the login page a logged-in session would be established and Craft would load the CP.

Once the CP loaded, Craft would throw an error ("An unknown error occurred") and subsequent attempts to navigate through the CP would throw the same error and content such as lists of entries would not appear. Further investigation of network request showed that `/actions` (POST, GET) requests were failing with `400 Bad Request`, with a response of `"Request must accept JSON in response"`.

Initially, before I happened upon actual bad request response details, the folks at Craft thought the issue might pertain to an inconsistent use of HTTPS, or something specific to the use of the Nginx proxy server.

As a test, however, I added `application/json` to the `HTTP_ACCEPT` header as follows:

`SetEnv HTTP_ACCEPT text/html,application/xhtml+xml,application/xml,application/json;q=0.9,*/*;q=0.8`

Low and behold, I was then able to successfully log in and navigate the CP. All of the POST and GET requests to `/actions` responded successfully.

Now, however, I've ended up in spot where saving an entry successfully commits the change to the database but renders the raw JSON response of the POST request on the page rather than returning to the entry's edit page. Here's an example of what I see in the browser when saving an entry:

`{"success":true,"id":15107,"title":"test","slug":"test","cpEditUrl":"https://my.domain.com/operations/entries/generalPages/15107-test","authorUsername":"admin","dateCreated":"2018-05-02T10:08:10-05:00","dateUpdated":"2018-05-02T10:08:10-05:00","postDate":null}`

The url in the browser remains on the entry such that if I refresh the page, I'm returned to the entry in the CP and see the updated content.

At this point, I'm stuck and not sure if the `HTTP_ACCEPT` header is configured incorrectly, or if the Nginx proxy server is indeed interfering with the requests.

Many thanks in advance for thoughts/assistance.

**Tags:** craft3, server, json, nginx, apache


---

## 1 answers

### Answer by username_1 on 2018-05-09. Score: 2 | Accepted answer

IMO, the issue is with this line.
SetEnv HTTP_ACCEPT text/html,application/xhtml+xml,application/xml,application/json;q=0.9,*/*;q=0.8

With ACCEPT headers set to a fixed value, it's impossible for Craft 3 to distinguish between Json and HTML requests. This must have been a necessity with the nginx proxy setup.
I suggest removing this line from Apache and configure nginx to pass through the original request headers.
If you use proxy_pass, trying adding this line
location / {
    ...
    proxy_pass_request_headers on;
    ...
}

OR, if you prefer a more direct approach
location / {
    ...
    proxy_set_header Accept $http_accept;
    ...
}


---

