nginx reverse proxy with named location: how to override backend header?How to set up Nginx as a caching reverse proxy?Help needed setting up nginx to serve static filesNginx reverse proxy: Not setting expires headerNginx reverse proxy + URL rewritenginx redirect issue with upstream configurationForward Custom Header from Nginx Reverse Proxynginx proxy redirecting request to different proxynginx proxy_pass rewrite of response header locationHow to hide backend URL/URI with Nginx reverse proxyConfigure NGINX : How to handle 500 Error on upstream itself, While Nginx handle other 5xx errors
Can a rogue use Sneak Attack in a Darkness spell cast by another player?
When to remove insignificant variables?
Number of solutions mod p and Betti numbers
How can you guarantee that you won't change/quit job after just couple of months?
Methodology: Writing unit tests for another developer
RandomInteger with equal number of 1 and -1
Prime sieve in Python
Does Doppler effect happen instantly?
Is there any proof that high saturation and contrast makes a picture more appealing in social media?
Count All Possible Unique Combinations of Letters in a Word
How does DC work with natural 20?
Why does the Saturn V have standalone inter-stage rings?
Is there a term for the belief that "if it's legal, it's moral"?
What's currently blocking the construction of the wall between Mexico and the US?
I don't like coffee, neither beer. How to politely work my way around that in a business situation?
UK - Working without a contract. I resign and guy wants to sue me
Heavily limited premature compiler translates text into excecutable python code
What is "industrial ethernet"?
Why does Linux list NVMe drives as /dev/nvme0 instead of /dev/sda?
Concurrent normals conjecture
What can I do with a research project that is my university’s intellectual property?
How did Gollum enter Moria?
What are Elsa's reasons for selecting the Holy Grail on behalf of Donovan?
How long would it take to cross the Channel in 1890's?
nginx reverse proxy with named location: how to override backend header?
How to set up Nginx as a caching reverse proxy?Help needed setting up nginx to serve static filesNginx reverse proxy: Not setting expires headerNginx reverse proxy + URL rewritenginx redirect issue with upstream configurationForward Custom Header from Nginx Reverse Proxynginx proxy redirecting request to different proxynginx proxy_pass rewrite of response header locationHow to hide backend URL/URI with Nginx reverse proxyConfigure NGINX : How to handle 500 Error on upstream itself, While Nginx handle other 5xx errors
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I'm having a difficult time getting add_header
to work when try_files
is used to send requests to a named location which specifies a backend with proxy_pass
.
Basically, I want to override Content-Type
for a specific set of URIs, but don't want to use a map
to set a variable for it (since I'd like the types
and mime.types
mechanism to continue working as is), nor to set an extension on the URIs and add more types
declarations.
Here's a simplified version of my nginx.conf
:
http
include mime.types;
default_type text/html;
charset utf-8;
server anothertextfile)$
try_files false @backend;
add_header Content-Type 'text/plain' always;
# This has no effect either
# default_type text/plain;
I'm using ngx_aws_auth here, but I don't think that should matter.
The behavior I've seen with nginx 1.16.0 is that Content-Type
is not returned at all; not for the /textfile
location where I expect text/plain
, nor for any other URL where I expect text/html
because of the default_type
at the http
level. If I remove the proxy_hide_header Content-Type
line then I simply get the backend's header, which is what I want to override.
I understand the frankly unintuitive behavior of add_header
where headers aren't inherited from a higher level if add_header
is specified at the current level, which I don't think is happening here, but I also tried moving all header directives to a standalone .conf file and including it everywhere, and I still get the same behavior.
I also tried using the headers-more module, with no difference.
What am I missing, Server Fault?
Thanks!
nginx reverse-proxy http-headers
add a comment |
I'm having a difficult time getting add_header
to work when try_files
is used to send requests to a named location which specifies a backend with proxy_pass
.
Basically, I want to override Content-Type
for a specific set of URIs, but don't want to use a map
to set a variable for it (since I'd like the types
and mime.types
mechanism to continue working as is), nor to set an extension on the URIs and add more types
declarations.
Here's a simplified version of my nginx.conf
:
http
include mime.types;
default_type text/html;
charset utf-8;
server anothertextfile)$
try_files false @backend;
add_header Content-Type 'text/plain' always;
# This has no effect either
# default_type text/plain;
I'm using ngx_aws_auth here, but I don't think that should matter.
The behavior I've seen with nginx 1.16.0 is that Content-Type
is not returned at all; not for the /textfile
location where I expect text/plain
, nor for any other URL where I expect text/html
because of the default_type
at the http
level. If I remove the proxy_hide_header Content-Type
line then I simply get the backend's header, which is what I want to override.
I understand the frankly unintuitive behavior of add_header
where headers aren't inherited from a higher level if add_header
is specified at the current level, which I don't think is happening here, but I also tried moving all header directives to a standalone .conf file and including it everywhere, and I still get the same behavior.
I also tried using the headers-more module, with no difference.
What am I missing, Server Fault?
Thanks!
nginx reverse-proxy http-headers
1
Have you tried switching the lineadd_header Content-Type 'text/plain' always;
tolocation @backend
block?
– Pothi Kalimuthu
Jun 5 at 4:07
1
@PothiKalimuthu That would set 'text/plain' for all backend responses, which I don't want to do. Again, I want to serve 'text/html' as default, and 'text/plain' only for a few specific URIs. In both cases this needs to override the backend's Content-Type.
– imiric
Jun 5 at 8:52
Okay. Got it. It was my misunderstanding, sorry. In that case, you could resolve the issue by having the exact replica oflocation @backend
block into something likelocation @custom_backend
with an extra lineadd_header Content-Type 'text/plain' always;
in the custom_backend block. If this is confusing, please see my answer with the sample code.
– Pothi Kalimuthu
Jun 6 at 2:31
add a comment |
I'm having a difficult time getting add_header
to work when try_files
is used to send requests to a named location which specifies a backend with proxy_pass
.
Basically, I want to override Content-Type
for a specific set of URIs, but don't want to use a map
to set a variable for it (since I'd like the types
and mime.types
mechanism to continue working as is), nor to set an extension on the URIs and add more types
declarations.
Here's a simplified version of my nginx.conf
:
http
include mime.types;
default_type text/html;
charset utf-8;
server anothertextfile)$
try_files false @backend;
add_header Content-Type 'text/plain' always;
# This has no effect either
# default_type text/plain;
I'm using ngx_aws_auth here, but I don't think that should matter.
The behavior I've seen with nginx 1.16.0 is that Content-Type
is not returned at all; not for the /textfile
location where I expect text/plain
, nor for any other URL where I expect text/html
because of the default_type
at the http
level. If I remove the proxy_hide_header Content-Type
line then I simply get the backend's header, which is what I want to override.
I understand the frankly unintuitive behavior of add_header
where headers aren't inherited from a higher level if add_header
is specified at the current level, which I don't think is happening here, but I also tried moving all header directives to a standalone .conf file and including it everywhere, and I still get the same behavior.
I also tried using the headers-more module, with no difference.
What am I missing, Server Fault?
Thanks!
nginx reverse-proxy http-headers
I'm having a difficult time getting add_header
to work when try_files
is used to send requests to a named location which specifies a backend with proxy_pass
.
Basically, I want to override Content-Type
for a specific set of URIs, but don't want to use a map
to set a variable for it (since I'd like the types
and mime.types
mechanism to continue working as is), nor to set an extension on the URIs and add more types
declarations.
Here's a simplified version of my nginx.conf
:
http
include mime.types;
default_type text/html;
charset utf-8;
server anothertextfile)$
try_files false @backend;
add_header Content-Type 'text/plain' always;
# This has no effect either
# default_type text/plain;
I'm using ngx_aws_auth here, but I don't think that should matter.
The behavior I've seen with nginx 1.16.0 is that Content-Type
is not returned at all; not for the /textfile
location where I expect text/plain
, nor for any other URL where I expect text/html
because of the default_type
at the http
level. If I remove the proxy_hide_header Content-Type
line then I simply get the backend's header, which is what I want to override.
I understand the frankly unintuitive behavior of add_header
where headers aren't inherited from a higher level if add_header
is specified at the current level, which I don't think is happening here, but I also tried moving all header directives to a standalone .conf file and including it everywhere, and I still get the same behavior.
I also tried using the headers-more module, with no difference.
What am I missing, Server Fault?
Thanks!
nginx reverse-proxy http-headers
nginx reverse-proxy http-headers
asked Jun 4 at 21:27
imiricimiric
11814
11814
1
Have you tried switching the lineadd_header Content-Type 'text/plain' always;
tolocation @backend
block?
– Pothi Kalimuthu
Jun 5 at 4:07
1
@PothiKalimuthu That would set 'text/plain' for all backend responses, which I don't want to do. Again, I want to serve 'text/html' as default, and 'text/plain' only for a few specific URIs. In both cases this needs to override the backend's Content-Type.
– imiric
Jun 5 at 8:52
Okay. Got it. It was my misunderstanding, sorry. In that case, you could resolve the issue by having the exact replica oflocation @backend
block into something likelocation @custom_backend
with an extra lineadd_header Content-Type 'text/plain' always;
in the custom_backend block. If this is confusing, please see my answer with the sample code.
– Pothi Kalimuthu
Jun 6 at 2:31
add a comment |
1
Have you tried switching the lineadd_header Content-Type 'text/plain' always;
tolocation @backend
block?
– Pothi Kalimuthu
Jun 5 at 4:07
1
@PothiKalimuthu That would set 'text/plain' for all backend responses, which I don't want to do. Again, I want to serve 'text/html' as default, and 'text/plain' only for a few specific URIs. In both cases this needs to override the backend's Content-Type.
– imiric
Jun 5 at 8:52
Okay. Got it. It was my misunderstanding, sorry. In that case, you could resolve the issue by having the exact replica oflocation @backend
block into something likelocation @custom_backend
with an extra lineadd_header Content-Type 'text/plain' always;
in the custom_backend block. If this is confusing, please see my answer with the sample code.
– Pothi Kalimuthu
Jun 6 at 2:31
1
1
Have you tried switching the line
add_header Content-Type 'text/plain' always;
to location @backend
block?– Pothi Kalimuthu
Jun 5 at 4:07
Have you tried switching the line
add_header Content-Type 'text/plain' always;
to location @backend
block?– Pothi Kalimuthu
Jun 5 at 4:07
1
1
@PothiKalimuthu That would set 'text/plain' for all backend responses, which I don't want to do. Again, I want to serve 'text/html' as default, and 'text/plain' only for a few specific URIs. In both cases this needs to override the backend's Content-Type.
– imiric
Jun 5 at 8:52
@PothiKalimuthu That would set 'text/plain' for all backend responses, which I don't want to do. Again, I want to serve 'text/html' as default, and 'text/plain' only for a few specific URIs. In both cases this needs to override the backend's Content-Type.
– imiric
Jun 5 at 8:52
Okay. Got it. It was my misunderstanding, sorry. In that case, you could resolve the issue by having the exact replica of
location @backend
block into something like location @custom_backend
with an extra line add_header Content-Type 'text/plain' always;
in the custom_backend block. If this is confusing, please see my answer with the sample code.– Pothi Kalimuthu
Jun 6 at 2:31
Okay. Got it. It was my misunderstanding, sorry. In that case, you could resolve the issue by having the exact replica of
location @backend
block into something like location @custom_backend
with an extra line add_header Content-Type 'text/plain' always;
in the custom_backend block. If this is confusing, please see my answer with the sample code.– Pothi Kalimuthu
Jun 6 at 2:31
add a comment |
1 Answer
1
active
oldest
votes
The solution is to use an additional named block to have a custom header for all the requests pass through it. The code in the original question didn't work because add_header is effective only on the last matched location block. If a request passes through multiple location blocks, it doesn't pick up add_header
directive on the passed location blocks. Nginx only considers or looks for add_header
in the last matched location block. In this case, the named location block is the last matched location block. I hope that clarifies why the original code didn't work as expected.
http
include mime.types;
default_type text/html;
charset utf-8;
server
listen 80;
location @backend
rewrite ^ /proxy$uri break;
proxy_pass https://backend;
proxy_intercept_errors on;
aws_access_key ***;
aws_secret_key ***;
s3_bucket ***;
chop_prefix /proxy;
proxy_set_header Authorization $s3_auth_token;
proxy_set_header x-amz-date $aws_date;
proxy_hide_header Content-Type;
location @plain_backend
rewrite ^ /proxy$uri break;
proxy_pass https://backend;
proxy_intercept_errors on;
aws_access_key ***;
aws_secret_key ***;
s3_bucket ***;
chop_prefix /proxy;
proxy_set_header Authorization $s3_auth_token;
proxy_set_header x-amz-date $aws_date;
proxy_hide_header Content-Type;
add_header Content-Type 'text/plain' always;
location /
try_files false @backend;
location ~ /(textfile
1
Thanks, that does indeed work. I was trying to avoid duplication, but that is easily remedied withinclude
. It's just unintuitive howadd_header
behaves, and ultimately I reverted to usingmap
to set a customContent-Type
variable, since there's seemingly no way to use this named location approach while retaining thetypes
/mime.types
functionality, as this poor fella wanted as well. It's surprising to me that something so basic (overriding backend headers) is so difficult to do with nginx. Thanks again!
– imiric
Jun 6 at 21:49
Good to know that you switched to map. There are indeed multiple ways to solve an issue with Nginx. None of them are clean and easy. However, with the introduction of njs, the future of Nginx customization looks promising. When njs becomes a standard module, there will be a paradigm shift on everything about Nginx.
– Pothi Kalimuthu
Jun 7 at 0:26
1
Interesting, I hadn't heard about njs. It sounds like Lua/OpenResty, and FWIW what I wanted to do can be accomplished with Lua, according to this comment, though I haven't tried it. I don't want to add that kind of extensibility/complexity when nginx should be capable of doing it OOB.
– imiric
Jun 7 at 10:37
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "2"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fserverfault.com%2fquestions%2f970139%2fnginx-reverse-proxy-with-named-location-how-to-override-backend-header%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The solution is to use an additional named block to have a custom header for all the requests pass through it. The code in the original question didn't work because add_header is effective only on the last matched location block. If a request passes through multiple location blocks, it doesn't pick up add_header
directive on the passed location blocks. Nginx only considers or looks for add_header
in the last matched location block. In this case, the named location block is the last matched location block. I hope that clarifies why the original code didn't work as expected.
http
include mime.types;
default_type text/html;
charset utf-8;
server
listen 80;
location @backend
rewrite ^ /proxy$uri break;
proxy_pass https://backend;
proxy_intercept_errors on;
aws_access_key ***;
aws_secret_key ***;
s3_bucket ***;
chop_prefix /proxy;
proxy_set_header Authorization $s3_auth_token;
proxy_set_header x-amz-date $aws_date;
proxy_hide_header Content-Type;
location @plain_backend
rewrite ^ /proxy$uri break;
proxy_pass https://backend;
proxy_intercept_errors on;
aws_access_key ***;
aws_secret_key ***;
s3_bucket ***;
chop_prefix /proxy;
proxy_set_header Authorization $s3_auth_token;
proxy_set_header x-amz-date $aws_date;
proxy_hide_header Content-Type;
add_header Content-Type 'text/plain' always;
location /
try_files false @backend;
location ~ /(textfile
1
Thanks, that does indeed work. I was trying to avoid duplication, but that is easily remedied withinclude
. It's just unintuitive howadd_header
behaves, and ultimately I reverted to usingmap
to set a customContent-Type
variable, since there's seemingly no way to use this named location approach while retaining thetypes
/mime.types
functionality, as this poor fella wanted as well. It's surprising to me that something so basic (overriding backend headers) is so difficult to do with nginx. Thanks again!
– imiric
Jun 6 at 21:49
Good to know that you switched to map. There are indeed multiple ways to solve an issue with Nginx. None of them are clean and easy. However, with the introduction of njs, the future of Nginx customization looks promising. When njs becomes a standard module, there will be a paradigm shift on everything about Nginx.
– Pothi Kalimuthu
Jun 7 at 0:26
1
Interesting, I hadn't heard about njs. It sounds like Lua/OpenResty, and FWIW what I wanted to do can be accomplished with Lua, according to this comment, though I haven't tried it. I don't want to add that kind of extensibility/complexity when nginx should be capable of doing it OOB.
– imiric
Jun 7 at 10:37
add a comment |
The solution is to use an additional named block to have a custom header for all the requests pass through it. The code in the original question didn't work because add_header is effective only on the last matched location block. If a request passes through multiple location blocks, it doesn't pick up add_header
directive on the passed location blocks. Nginx only considers or looks for add_header
in the last matched location block. In this case, the named location block is the last matched location block. I hope that clarifies why the original code didn't work as expected.
http
include mime.types;
default_type text/html;
charset utf-8;
server
listen 80;
location @backend
rewrite ^ /proxy$uri break;
proxy_pass https://backend;
proxy_intercept_errors on;
aws_access_key ***;
aws_secret_key ***;
s3_bucket ***;
chop_prefix /proxy;
proxy_set_header Authorization $s3_auth_token;
proxy_set_header x-amz-date $aws_date;
proxy_hide_header Content-Type;
location @plain_backend
rewrite ^ /proxy$uri break;
proxy_pass https://backend;
proxy_intercept_errors on;
aws_access_key ***;
aws_secret_key ***;
s3_bucket ***;
chop_prefix /proxy;
proxy_set_header Authorization $s3_auth_token;
proxy_set_header x-amz-date $aws_date;
proxy_hide_header Content-Type;
add_header Content-Type 'text/plain' always;
location /
try_files false @backend;
location ~ /(textfile
1
Thanks, that does indeed work. I was trying to avoid duplication, but that is easily remedied withinclude
. It's just unintuitive howadd_header
behaves, and ultimately I reverted to usingmap
to set a customContent-Type
variable, since there's seemingly no way to use this named location approach while retaining thetypes
/mime.types
functionality, as this poor fella wanted as well. It's surprising to me that something so basic (overriding backend headers) is so difficult to do with nginx. Thanks again!
– imiric
Jun 6 at 21:49
Good to know that you switched to map. There are indeed multiple ways to solve an issue with Nginx. None of them are clean and easy. However, with the introduction of njs, the future of Nginx customization looks promising. When njs becomes a standard module, there will be a paradigm shift on everything about Nginx.
– Pothi Kalimuthu
Jun 7 at 0:26
1
Interesting, I hadn't heard about njs. It sounds like Lua/OpenResty, and FWIW what I wanted to do can be accomplished with Lua, according to this comment, though I haven't tried it. I don't want to add that kind of extensibility/complexity when nginx should be capable of doing it OOB.
– imiric
Jun 7 at 10:37
add a comment |
The solution is to use an additional named block to have a custom header for all the requests pass through it. The code in the original question didn't work because add_header is effective only on the last matched location block. If a request passes through multiple location blocks, it doesn't pick up add_header
directive on the passed location blocks. Nginx only considers or looks for add_header
in the last matched location block. In this case, the named location block is the last matched location block. I hope that clarifies why the original code didn't work as expected.
http
include mime.types;
default_type text/html;
charset utf-8;
server
listen 80;
location @backend
rewrite ^ /proxy$uri break;
proxy_pass https://backend;
proxy_intercept_errors on;
aws_access_key ***;
aws_secret_key ***;
s3_bucket ***;
chop_prefix /proxy;
proxy_set_header Authorization $s3_auth_token;
proxy_set_header x-amz-date $aws_date;
proxy_hide_header Content-Type;
location @plain_backend
rewrite ^ /proxy$uri break;
proxy_pass https://backend;
proxy_intercept_errors on;
aws_access_key ***;
aws_secret_key ***;
s3_bucket ***;
chop_prefix /proxy;
proxy_set_header Authorization $s3_auth_token;
proxy_set_header x-amz-date $aws_date;
proxy_hide_header Content-Type;
add_header Content-Type 'text/plain' always;
location /
try_files false @backend;
location ~ /(textfile
The solution is to use an additional named block to have a custom header for all the requests pass through it. The code in the original question didn't work because add_header is effective only on the last matched location block. If a request passes through multiple location blocks, it doesn't pick up add_header
directive on the passed location blocks. Nginx only considers or looks for add_header
in the last matched location block. In this case, the named location block is the last matched location block. I hope that clarifies why the original code didn't work as expected.
http
include mime.types;
default_type text/html;
charset utf-8;
server
listen 80;
location @backend
rewrite ^ /proxy$uri break;
proxy_pass https://backend;
proxy_intercept_errors on;
aws_access_key ***;
aws_secret_key ***;
s3_bucket ***;
chop_prefix /proxy;
proxy_set_header Authorization $s3_auth_token;
proxy_set_header x-amz-date $aws_date;
proxy_hide_header Content-Type;
location @plain_backend
rewrite ^ /proxy$uri break;
proxy_pass https://backend;
proxy_intercept_errors on;
aws_access_key ***;
aws_secret_key ***;
s3_bucket ***;
chop_prefix /proxy;
proxy_set_header Authorization $s3_auth_token;
proxy_set_header x-amz-date $aws_date;
proxy_hide_header Content-Type;
add_header Content-Type 'text/plain' always;
location /
try_files false @backend;
location ~ /(textfile
answered Jun 6 at 2:36
Pothi KalimuthuPothi Kalimuthu
3,94411934
3,94411934
1
Thanks, that does indeed work. I was trying to avoid duplication, but that is easily remedied withinclude
. It's just unintuitive howadd_header
behaves, and ultimately I reverted to usingmap
to set a customContent-Type
variable, since there's seemingly no way to use this named location approach while retaining thetypes
/mime.types
functionality, as this poor fella wanted as well. It's surprising to me that something so basic (overriding backend headers) is so difficult to do with nginx. Thanks again!
– imiric
Jun 6 at 21:49
Good to know that you switched to map. There are indeed multiple ways to solve an issue with Nginx. None of them are clean and easy. However, with the introduction of njs, the future of Nginx customization looks promising. When njs becomes a standard module, there will be a paradigm shift on everything about Nginx.
– Pothi Kalimuthu
Jun 7 at 0:26
1
Interesting, I hadn't heard about njs. It sounds like Lua/OpenResty, and FWIW what I wanted to do can be accomplished with Lua, according to this comment, though I haven't tried it. I don't want to add that kind of extensibility/complexity when nginx should be capable of doing it OOB.
– imiric
Jun 7 at 10:37
add a comment |
1
Thanks, that does indeed work. I was trying to avoid duplication, but that is easily remedied withinclude
. It's just unintuitive howadd_header
behaves, and ultimately I reverted to usingmap
to set a customContent-Type
variable, since there's seemingly no way to use this named location approach while retaining thetypes
/mime.types
functionality, as this poor fella wanted as well. It's surprising to me that something so basic (overriding backend headers) is so difficult to do with nginx. Thanks again!
– imiric
Jun 6 at 21:49
Good to know that you switched to map. There are indeed multiple ways to solve an issue with Nginx. None of them are clean and easy. However, with the introduction of njs, the future of Nginx customization looks promising. When njs becomes a standard module, there will be a paradigm shift on everything about Nginx.
– Pothi Kalimuthu
Jun 7 at 0:26
1
Interesting, I hadn't heard about njs. It sounds like Lua/OpenResty, and FWIW what I wanted to do can be accomplished with Lua, according to this comment, though I haven't tried it. I don't want to add that kind of extensibility/complexity when nginx should be capable of doing it OOB.
– imiric
Jun 7 at 10:37
1
1
Thanks, that does indeed work. I was trying to avoid duplication, but that is easily remedied with
include
. It's just unintuitive how add_header
behaves, and ultimately I reverted to using map
to set a custom Content-Type
variable, since there's seemingly no way to use this named location approach while retaining the types
/mime.types
functionality, as this poor fella wanted as well. It's surprising to me that something so basic (overriding backend headers) is so difficult to do with nginx. Thanks again!– imiric
Jun 6 at 21:49
Thanks, that does indeed work. I was trying to avoid duplication, but that is easily remedied with
include
. It's just unintuitive how add_header
behaves, and ultimately I reverted to using map
to set a custom Content-Type
variable, since there's seemingly no way to use this named location approach while retaining the types
/mime.types
functionality, as this poor fella wanted as well. It's surprising to me that something so basic (overriding backend headers) is so difficult to do with nginx. Thanks again!– imiric
Jun 6 at 21:49
Good to know that you switched to map. There are indeed multiple ways to solve an issue with Nginx. None of them are clean and easy. However, with the introduction of njs, the future of Nginx customization looks promising. When njs becomes a standard module, there will be a paradigm shift on everything about Nginx.
– Pothi Kalimuthu
Jun 7 at 0:26
Good to know that you switched to map. There are indeed multiple ways to solve an issue with Nginx. None of them are clean and easy. However, with the introduction of njs, the future of Nginx customization looks promising. When njs becomes a standard module, there will be a paradigm shift on everything about Nginx.
– Pothi Kalimuthu
Jun 7 at 0:26
1
1
Interesting, I hadn't heard about njs. It sounds like Lua/OpenResty, and FWIW what I wanted to do can be accomplished with Lua, according to this comment, though I haven't tried it. I don't want to add that kind of extensibility/complexity when nginx should be capable of doing it OOB.
– imiric
Jun 7 at 10:37
Interesting, I hadn't heard about njs. It sounds like Lua/OpenResty, and FWIW what I wanted to do can be accomplished with Lua, according to this comment, though I haven't tried it. I don't want to add that kind of extensibility/complexity when nginx should be capable of doing it OOB.
– imiric
Jun 7 at 10:37
add a comment |
Thanks for contributing an answer to Server Fault!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fserverfault.com%2fquestions%2f970139%2fnginx-reverse-proxy-with-named-location-how-to-override-backend-header%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Have you tried switching the line
add_header Content-Type 'text/plain' always;
tolocation @backend
block?– Pothi Kalimuthu
Jun 5 at 4:07
1
@PothiKalimuthu That would set 'text/plain' for all backend responses, which I don't want to do. Again, I want to serve 'text/html' as default, and 'text/plain' only for a few specific URIs. In both cases this needs to override the backend's Content-Type.
– imiric
Jun 5 at 8:52
Okay. Got it. It was my misunderstanding, sorry. In that case, you could resolve the issue by having the exact replica of
location @backend
block into something likelocation @custom_backend
with an extra lineadd_header Content-Type 'text/plain' always;
in the custom_backend block. If this is confusing, please see my answer with the sample code.– Pothi Kalimuthu
Jun 6 at 2:31