I have a reverse proxy server that takes file from fileserver than sends them to user. I want to change filename before sending. I wrote a rule like below
location ~ downloads/(.*) {
proxy_pass http://HOST:PORT/remote.php/dav/files/$1;
add_header Content-Disposition 'attachment; "filename=$args"';
}
But when i send request i get this error;
ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION
Adding a header with add_header works well with proxy pass, but if there is an existing header value in the response it will stack the values.
If you want to replace a header value (for example replace this Content-Disposition then you will have to do this in two steps:
# 1. hide the Content-Disposition from the server response
proxy_hide_header Content-Disposition;
# 2. add a new Content-Disposition header with your changes
add_header Content-Disposition 'attachment; "filename=$args"';
See my answer on a similar question here.
Related
I am following nginx - read custom header from upstream server . My requirement is to read a http header and send it as a cookie to upstream server
So I have following code in location block
if ($http_remote_user){
add_header Set-Cookie UCM_INFO=$http_remote_user;
}
I can see in logs(error.log) that I am getting the value of remote_user but the above mentioned block is not getting executed.What could be the issue?
Posting the answer so that it helps others.I was able to figure out the solution.Since I had unserscores in headers so nginx was rejecting the headers.
So we need to put below in conf file
underscores_in_headers on
Restarted the server and viola it worked.
I understand that I can use one of the following to remove specific headers from the response in NGINX
proxy_hide_header
more_clear_headers
proxy_hide_header
more_clear_headers
However, As I mentioned, my requirement is to remove all of the response headers. Above directives will only help in removing specified headers.
Is there any way I can remove all headers before sending the response?
more_clear_headers supports wildcard *
so this works for all X-Headers: more_clear_headers 'X-*';
Never tried but I assume that also more_clear_headers '*'; works.
But you have to build from source for using headers-more-nginx-modul.
I am having trouble making cross site ajax calls. Using nginx, I believe I have added the right headers in the server configuration, but it still does not work in my JS code or in the console. I wonder what I am doing wrong.
Here is what I type into the console the response is the familiar "No 'Access-Control-Allow-Origin' header is present" error:
$.get("//www.example.com");
Object {readyState: 1, getResponseHeader: function, getAllResponseHeaders: function, setRequestHeader: function, overrideMimeType: function…}
XMLHttpRequest cannot load http://www.example.com/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://dictionary.aherriot.com' is therefore not allowed access.
When I look at the response headers from my initial webpage load, I do see the following headers:
Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:X-Requested-With,Accept,Content-Type, Origin
Access-Control-Allow-Methods:GET, POST, OPTIONS, PUT, DELETE
Access-Control-Allow-Origin:*
and here is the nginx config file for the site:
server {
listen 80;
root /home/aherriot/sites/dictionary;
index index.html index.htm;
server_name dictionary.aherriot.com;
location / {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
add_header 'Access-Control-Allow-Headers' 'X-Requested-With,Accept,Content-Type, Origin';
}
}
I am stumped as to what I am missing. What else do I need to do to allow CORS? Thank you.
You can use * to accept all origin but it will prevent using 'Access-Control-Allow-Credentials: true'.
Take a look here for more examples - http://enable-cors.org/server_nginx.html
The problem with the question is there is a fundamental misunderstanding of what CORS is. It is not for the origin server to specify which third-party domains a page can access through XMLHttpRequest requests, but rather it is the server on an external domain that specifies which domains can connect to it.
I am looking for a nginx config setup that does setup the Access-Control-Allow-Origin to the value received in the Origin.
It seems that the * method doesn't work with Chrome and the multiple URLs doesn't work with Firefox as it is not allowed by CORS specification.
So far, the only solution is to setup the Access-Control-Allow-Origin to the value received in the origin (yes some validation could be implemented).
The question is how to do this in nginx, preferably without installing additional extensions.
set $allow_origin "https://example.com"
# instead I want to get the value from Origin request header
add_header 'Access-Control-Allow-Origin' $allow_origin;
Using if can sometimes break other config such as try_files. You can end up with unexpected 404s.
Use map instead
map $http_origin $cors_header {
default "";
"~^https?://[^/]+\.example\.com(:[0-9]+)?$" "$http_origin";
}
server {
...
location / {
add_header Access-Control-Allow-Origin $cors_header;
try_files $uri $uri/ /index.php;
}
...
}
If is evil
I'm starting to use this myself, and this is the line in my current Nginx configuration:
add_header 'Access-Control-Allow-Origin' "$http_origin";
This sets a header to allow the origin of the request as the only allowed origin. So where ever you are coming from is the only place allowed. So it shouldn't be much different than allowing "*" but it looks more specific from the browser's perspective.
Additionally you can use conditional logic in your Nginx config to specify a whitelist of hostnames to allow. Here's an example from https://gist.github.com/Ry4an/6195025
if ($http_origin ~* (whitelist\.address\.one|whitelist\.address\.two)$) {
add_header Access-Control-Allow-Origin "$http_origin";
}
I plan to try this technique in my own server to whitelist the allowed domains.
Here is a part of a file from conf.f directory where people always describes their virtual hosts of Nginx.
$http_origin compares with list of allowed_origins and then in second map block the system decides what will write to "header Access-Control-Allow-Origin" according to allowed list.
Here is a part of code.
#cat /etc/nginx/conf.d/somehost.conf
map $http_origin $origin_allowed {
default 0;
https://xxxx.yyyy.com 1;
https://zzz.yyyy.com 1;
}
map $origin_allowed $origin {
default "";
1 $http_origin;
}
server {
server_name somehost.com;
#...[skipped text]
add_header Access-Control-Allow-Origin $origin always;
#....[skipped text]
}
I test it om my servers. All works fine.
Have a nice day & be healthy,
Eugene.
I want to add a custom header for the response received from the server behind nginx.
While add_header works for nginx-processed responses, it does nothing when the proxy_pass is used.
add_header works as well with proxy_pass as without. I just today set up a configuration where I've used exactly that directive. I have to admit though that I've struggled as well setting this up without exactly recalling the reason, though.
Right now I have a working configuration and it contains the following (among others):
server {
server_name .myserver.com
location / {
proxy_pass http://mybackend;
add_header X-Upstream $upstream_addr;
}
}
Before nginx 1.7.5 add_header worked only on successful responses, in contrast to the HttpHeadersMoreModule mentioned by Sebastian Goodman in his answer.
Since nginx 1.7.5 you can use the keyword always to include custom headers even in error responses. For example:
add_header X-Upstream $upstream_addr always;
Limitation: You cannot override the server header value using add_header.
Hide response header and then add a new custom header value
Adding a header with add_header works fine with proxy pass, but if there is an existing header value in the response it will stack the values.
If you want to set or replace a header value (for example replace the Access-Control-Allow-Origin header to match your client for allowing cross origin resource sharing) then you can do as follows:
# 1. hide the Access-Control-Allow-Origin from the server response
proxy_hide_header Access-Control-Allow-Origin;
# 2. add a new custom header that allows all * origins instead
add_header Access-Control-Allow-Origin *;
So proxy_hide_header combined with add_header gives you the power to set/replace response header values.
Similar answer can be found here on ServerFault
UPDATE:
Note: proxy_set_header is for setting request headers before the request is sent further, not for setting response headers (these configuration attributes for headers can be a bit confusing).
As oliver writes:
add_header works as well with proxy_pass as without.
However, as Shane writes, as of Nginx 1.7.5, you must pass always in order to get add_header to work for error responses, like so:
add_header X-Upstream $upstream_addr always;
There is a module called HttpHeadersMoreModule that gives you more control over headers. It does not come with Nginx and requires additional installation. With it, you can do something like this:
location ... {
more_set_headers "Server: my_server";
}
That will "set the Server output header to the custom value for any status code and any content type". It will replace headers that are already set or add them if unset.
You could try this solution :
In your location block when you use proxy_pass do something like this:
location ... {
add_header yourHeaderName yourValue;
proxy_pass xxxx://xxx_my_proxy_addr_xxx;
# Now use this solution:
proxy_ignore_headers yourHeaderName // but set by proxy
# Or if above didn't work maybe this:
proxy_hide_header yourHeaderName // but set by proxy
}
I'm not sure would it be exactly what you need but try some manipulation of this method and maybe result will fit your problem.
Also you can use this combination:
proxy_hide_header headerSetByProxy;
set $sent_http_header_set_by_proxy yourValue;