Hi I'm running Laravel on NGINX server and I would like to use NGINX reverse proxy capability as an API gateway for my Laravel and other node API application. Here are my configurations:
Application URL: staging-app.example.com
Application API Endpoint: staging-app.example.com/api
API Gateway URL: api.example.com
What I want to do, is to redirect all API requests api.example.com/staging-app to staging-app.example.com/api. I have succeed in redirecting the API request, but somehow the Authorization header is not passed along to the proxy pass resulting in 401 unauthorized while other header do get passed along.
Here is my current api.example.com nginx config:
server { server_name api.example.com; location /staging-app { rewrite ^/staging-app/(.*)$ /$1 break; proxy_pass http://staging-app.example.com/; } location /test { rewrite ^/test/(.*)$ /$1 break; proxy_pass http://127.0.0.1:3333/; } listen [::]:443 ssl; # managed by Certbot listen 443 ssl; # managed by Certbot ssl_certificate /etc/letsencrypt/live/api.example.com/fullchain.pem; # managed by Certbot ssl_certificate_key /etc/letsencrypt/live/api.example.com/privkey.pem; # managed by Certbot include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot } server { if ($host = api.example.com) { return 301 https://$host$request_uri; } # managed by Certbot listen 80; listen [::]:80; server_name api.example.com; return 404; # managed by Certbot } and for my laravel application, I use the configuration given from Laravel themselves
Update 1: I tried adding proxy_set_header Test testingvalue in the location block directly, but it doesn't seems to work either
Authorizationheader to your upstream. However you are not passing your request to the/apiendpoint; to do it, uselocation /staging-app { proxy_pass http://staging-app.example.com/api; }instead. Actually, no rewrite rules are required for your configuration to strip the URI prefix at all; check this SO thread to find out why.Authorization header. When I try adding another header such asauthorizationzzit get passed through.Authorizationheader is passing correctly, so I think it is a Laravel who is in charge of it's disappearing.Authorizationheader can be lost if you are 1) requesting auth and passing theAuthorizationheader using different protocols (HTTP/HTTPS); 2) receiving a redirect (see related Stack Overflow threads: 1, 2); 3) dealing with the CORSOPTIONSrequest (see related Stack Overflow thread).