I am unable to get the complete URL using var fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl;
suppose I am calling this on route /getUrl
I should get http://<server_ip>/app/users/happy/getUrl
instead I only get http://<server_ip>/getUrl
I am using Nginx as a reverse proxy.
location /app/users/happy/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:4216/;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header X-Forwarded-Server $http_host;
proxy_redirect ~/(.*)$ /app/users/happy/$1;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
I also tried adding
app.set('trust proxy', 'loopback');
The proxy converts a request
GET http://<proxy_ip>/app/users/happy/getUrl HTTP/1.1
Host: <proxy_ip>
into
GET http://<server_ip>/getUrl HTTP/1.1
Host: <server_ip>
X-Forwarded-Host: <server_ip>
X-<other headers mentioned in the nginx directive>
Therefore the prefix /app/users/happy never reaches your server. You could add it in yet another header:
proxy_set_header X-Path-Prefix /app/users/happy
and obtain it as req.get("X-Path-Prefix"), but what do you need it for?
Related
i am new in nginx and i cant access my public files please guide me i am very new on doing this.
this is my code on express js
app.use('/p',auth, express.static('public/uploads'),
serveIndex('public/uploads', {'icons': true}))
and this is the code on nginx
location =/file/{
proxy_pass http://localhost:8081/p/;
}
it does work like this on my page when i go to http://mywebsite.com/file/ but when we click a file here which will show that directory. it will error out or stay white screen
here is the full code of the location
#this is the file system
location =/file/{
proxy_pass http://localhost:8081/p/;
}
#this is the express api
location /v1
{
rewrite ^/v1/(.*)?$ /$1 break;
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://localhost:8081;
}
#this is the front end
location /
{
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://localhost:5000;
}
I am using Express for serving static files and dynamic URLs on my nodejs app. I am generating a URL based on the :id in Mongodb and then on NGINX I am using Nodejs reverse proxy to serve this path to the user but it doesn't work whatever I try. It always says "CANNOT GET.."
I am running NGINX 1.14.2, Node 8.14.0. I have tried NGINX rewrite directive and various wild cards ( ~, ~^, ., etc.) but none of them have worked. I have included the current express code and NGINX config below. Any help is hugely appreciated
Express
router.get('/dynamic-uri/:fooId', function (req, res) {
res.render('index');
});
The resultant URL with :fooId is
https://example.com/myNodeApp/dynamic-uri/5c35e51228122504b57126d7
NGINX Config
location /myNodeApp/dynamic-uri {
proxy_pass http://x.x.x.x:3000;
proxy_http_version 1.1;
proxy_redirect off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header X-NginX-Proxy true;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Real-IP $remote_addr;
}
Of course the :fooId doesn't exist as a static path and to serve static files i have the following block which works fine
location ~* /myNodeApp/(static|img|media|app|css|js) {
proxy_pass http://x.x.x.x:3000;
proxy_http_version 1.1;
proxy_redirect off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Real-IP $remote_addr;
}
i have a laravel app and i want all requests to domain.test/api to be proxied to nodeJs but only if it is an xhr request. meaning that if a user types in a browser domain.test/api i want to give him a 404 but if the request is made with ajax i want to give him the response.
the following configuration proxies all:
location ~* ^\/api(.*)$ {
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 300;
proxy_pass http://localhost:8081;
}
Is what i want to do possible using nginx? if so, please do suggest your solutions?
I will make it short without introductions ..
I'm having a serious issues with NGINX configuration (on google cloud) to make 2 nodejs apps working on the same domain with different PORTS
let's say app1 is working on port 3002, app2 working on port 3003
app1
location / {
root /home/bitnami/project_name;
proxy_pass http://127.0.0.1:3002;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_redirect off;
}
app2
location /app2 {
root /home/bitnami/project_name;
proxy_pass http://127.0.0.1:3003;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_redirect off;
}
when I surf www.example.com/app2, I get 404 page
I know some of u will say that this Q has been asked before, believe me I've tried all possible solutions on stackoverflow .. non has worked with me
Note: app1 location it has to be the main domain so (/) the main domain URL without path
I believe your code does not use relative paths, thats why you are getting this error, add this line:
rewrite ^/app2(.*) /$1 break;
and no root required for proxy pass, your new code shall look like this:
location /app2 {
#root /home/bitnami/project_name;
proxy_pass http://127.0.0.1:3003;
#proxy_http_version 1.1;
#proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
#proxy_set_header X-NginX-Proxy true;
#proxy_redirect off;
rewrite ^/app2(.*) /$1 break;
}
The first location block captures requests for all requests of your domain, leaving the second block never used. Put the second block before the first one and it should work.
I have two node js applications I am running on the same box and I would like for it to run the first node js app for all routing except if the url is www.domain.com/blog to go to the other node js application. Is this even possible with this setup or do I have to setup subdomains and use nginx or something?
You can achieve this using nginx as a reverse proxy.
Assuming you have your blog node process running on port 3000 and another node process on 3001 a simple config would look like;
upstream blog {
server 127.0.0.1:3000;
}
upstream other {
server 127.0.0.1:3001;
}
server {
listen 80;
server_name www.domain.com;
location /blog {
proxy_pass http://blog;
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header X-Real-IP $proxy_protocol_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto tcp;
proxy_set_header X-NginX-Proxy true;
}
location / {
proxy_pass http://other;
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header X-Real-IP $proxy_protocol_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto tcp;
proxy_set_header X-NginX-Proxy true;
}
}