nginx overwrite custom response headers - nginx

I have an API endpoint that returns a PDF file on the header in my local using apache.
return of request:
The header response are created with the package mpdf/mpdf and apache in my local environment don't overwrite. But, when a do a deploy, the remote server is nginx, and overwrite all the headers that was setup from mpdf.
Nginx header response:
I searched and see this solution: nginx sets the wrong Content-Type header for dynamically generated files
But, at this moment, i can't perform a change on production config file of nginx.
I need to know if has another solution for this problem.

Related

Is there a way to dynamically change the base_url in a symfony project based on reverse proxy header

I'm working on an app, with the front(vueJS) and back(symfony with API-platform) separated on the same domain.
We have an NGINX reverse proxy that redirects url base on these rule :
domain.com/ -> front container on port 7000
domain.com/api -> back container on port 8000
domain.com/preview-mrXXX/ -> front container on port 7XXX
domain.com/api/preview-mrXXX/ -> back container on port 8XXX
The problem is that the symfony assets aren't found because the request are on the root url and not the api or preview url.
e.g:
request goes to https://example.com/bundles/apiplatform/web.png
instead of https://example.com/api/bundles/apiplatform/web.png
I could pass a base_url in some header with nginx but I still need to configure symfony to requests on /api/ rather than /.
You can set the base_url for assets in each container for the Framework configuration file. (from the documentation https://symfony.com/doc/current/reference/configuration/framework.html#base-urls).
Since you need it to be dynamic, you can create an .env.local file in each container with the base_url for that particular container. Then in the Framework configuration file mentioned above, you can set the value to %base_url%(the variable you created in the .env.local file) so that it will dynamically resolve to the correct value based on your env. You can refer to more information on env configuration here. https://symfony.com/doc/current/configuration.html
UPDATE: You can also set the value to a real environment variable (from your shell or OS) by using '%env(resolve:ENV_VAR)%' (therefore avoiding defining it in the .env.local file). Relevant documentation: https://symfony.com/doc/current/configuration.html#configuration-based-on-environment-variables

Add permission check before sending the request to the file server

I use nginx as my reverse proxy server to fetch image from the file server, which is also a web server.
It works well. but I want add permission check based on the url before send request to my file server. If has permission, just forwarding to my file server, or else just return 404.
BTW, I can't add the check in my file server for some reason.
I think I got the answer.
The module "http_auth_request_module" is what I want. https://www.nginx.com/resources/admin-guide/restricting-access-auth-request/

Nginx Lua Scripting

One of the really cool things about Nginx is that you can take control of what it does by injecting Lua script at various phases of request processing. I have successfully used the rewrite_by_lua/file directive to examine the body of the incoming request and inject extra request headers for downstream processing by PHP. e.g.
location /api{
rewrite_by_lua_file "/path/to/rewrite.lua";
lua_need_request_body "on";
}
and then in rewrite.lua
local uri = ngx.var.request_uri;
//examine the URI and inject additional headers
ngx.req.set_header('headerName','headerValue');
What I can also do at this stage is inject response headers. For example
local cookieData = "cookieName=value;path=/;";
ngx.header['Set-Cookie'] = cookieData;
No issues thus far but this is not quite what I want to do. The workflow I have in mind goes like this
Examine the URI & inject extra request headers.
My PHP scripts examine the extra request headers, process the incoming data as required and may inject extra response headers
I then want to examine the response headers in another Nginx Lua script and inject cookies at that stage.
Injecting extra response headers via my PHP script is no problem at all. I thought that in order to examine those extra headers I would just need to setup
location /api {
header_filter_by_lua_file "path/to/header.lua";
}
with header.lua doing things like
local cookieData = "cookieName=value;path=/;";
ngx.header['Set-Cookie'] = cookieData;
The principle sounds perfect. However, I find that my header_filter_by_lua_file directives just get ignored - no errors reported in the Nginx log when I reload the configuration.
I must be doing something wrong here but I cannot see what it might be. I am using nginx 1.6.2 on Ubuntu 14.10 (x64). Nginx having been installed with apt-get install nginx-extras.
I'd be most grateful to anyone who might be able to explain how to get header_filter_by_* functioning correctly.
It's not a feature of vanilla nginx. You need to install openresty (instead of nginx).
See http://openresty.org/#Download and then http://openresty.org/#Installation

Writing to log file based on path in Nginx

I want to create an HTTP nginx server which will take whatever POST request comes in
/some/path
will append the post body to some_internal_path/some/path.log
Where some and path would be variables. So /another/place would append to some_internal_path/another/place.log
How can I do this with nginx?

Can't login to gitorious installation, cookie and domain issues

I've installed gitorious on CentOS 6 32bit via getgetorious.com's installer. I went ahead and edited the gitorious.yml with my host name "git.mydomain.com". Restarted gitorious with /usr/bin/restart_gitorious.
I have checked the conf.d/gitorious.conf file for nginx and it shows the server name as my "git.mydomain.com".
When I try to login I get redirected back to the homepage and nothing happens. I checked the headers being sent from the server and the cookies are for "www.mydomain.com" instead of "git.mydomain.com"
I have a server block in my nginx.conf for "www.mydomain.com" which points to a completely different directory. I can't figure out how and why the cookies are being set to www instead of git.
Here's paste of my headers. I've replaced my actual domain with mydomain.com.
http://pastebin.com/Lu0txrtR
I'm also seeing this error in the production.log file
WARNING: Invalid request host 'git.fruition.company'. Session cookies
will not work
I have made the following changes to my gitorious.yml file.
# Host the ./script/gitorious script should use: client_host:
git.fruition.company
# Host which is serving the gitorious app, eg "gitorious.org"
host: git.fruition.company
I have also tried setting the hostname using the provided shell script to no avail.
I can't figure out where or why the cookies are being set to www instead of git.
Any help would be greatly appreciated.
I ended up running the install script again and create a new user. Everything seems to work now.

Resources