Reputation: 533
I went to similar questions but without any success.
Let say I have two node.js app turning on a server:
// App GoodMorning
var express = require('express');
app.post('/breakfast', function (req, res) {
console.log("Eating breakfast");
res.sendStatus(200);
});
app.get('/', function (req, res) {
res.send('GoodMorning');
});
app.listen(3000, function () {
console.log('GoodMorning app listening on port 3000!');
});
and
// App GoodEvening
var express = require('express');
app.post('/diner', function (req, res) {
console.log("Eating diner");
res.sendStatus(200);
});
app.get('/', function (req, res) {
res.send('GoodEvening');
});
app.listen(4000, function () {
console.log('GoodEvening app listening on port 4000!');
});
And let's say Nginx is used as a reverse proxy server. So it has to send requests to the correct port, right ? So the "magic" file is like that:
# HTTP - redirect all requests to HTTPS:
server {
listen 80;
listen [::]:80 default_server ipv6only=on;
return 301 https://$host$request_uri;
}
# HTTPS - proxy requests on to local Node.js app:
server {
listen 443;
server_name iamhungry.com;
ssl on;
# Use certificate and key provided by Let's Encrypt:
ssl_certificate /etc/letsencrypt/live/iamhungry.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/iamhungry.com/privkey.pem;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
# Pass requests for / to localhost:3000:
location / {
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:3000/;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
# Pass requests for /homepageevening to localhost:4000:
location /homepageevening {
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:4000/;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
# Pass requests for /diner to localhost/diner:4000:
location /diner {
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/diner:4000/;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
}
Then the following requests do the following results:
$ curl iamhungry.com
$ GoodMorning // OK
$ curl -X POST iamhungry.com/breakfast
--> I see "Eating brakfast" in the log file of breakfast.js // OK
$ curl iamhungry.com/homepageevening
$ GoodEvening // OK
$ curl -X POST iamhungry.com/diner -I
HTTP/1.1 301 Moved Permanently // why ?!
--> And I see nothing in the log file of evening.js // why ?!
I am not at ease with these proxy concepts. I went through the documentation of nginx and I found no help about that. I wonder if my way of understanding the way it works is correct.
Upvotes: 0
Views: 7967
Reputation: 533
OK thank you @RichardSmith. I had to fix the configuration file:
location /diner {
...
proxy_pass http://localhost:4000/diner;
And do my tests with curl http://iamhungry.com -I -L
instead of curl http://iamhungry.com -I
to actually follow the rerouting.
Here is what I was missing:
A 301 is not an error. I was doing my test in the terminal using curl http://iamhungry.com -I
but using the option -L curl http://iamhungry.com -I -L
allowed me to follow the redirection and then get the end of the line ! So 301 is in fact normal using nginx because redirecting is its role.
Port number is attached to the domain name. Thanks @RichardSmith.
from @RichardSmith link: [...]the part of a normalized request URI matching the location is replaced by a URI specified in the directive.
Upvotes: 1