req.url is localhost in production
Unanswered
Arboreal ant posted this in #help-forum
Arboreal antOP
I can access the website via my domain, when i do npm run start on my prod server it says that its running on localhost:80 but the site is reachable via the domain or the server ip. i consolelogged req.url and it gave me "http://localhost/api/auth/callback?code=AaH" im proxying it trough cloudflare if that makes any difference
35 Replies
Arboreal antOP
Bump
if you are self-hosting, try starting the server with
or use
npm run start --hostname yourdomain.comor use
req.header.get('host') instead@Ray where are you hosting the site?
Arboreal antOP
on a vps running ubuntu
not related to this issue but is it better to just use nextjs with pm2 or should i bring in nginx?
Arboreal antOP
whats the benefit of adding nginx?
@Arboreal ant whats the benefit of adding nginx?
nginx serve http better than node, and compression, http2
Arboreal antOP
i ran npm run start --hostname (my domain) and got
> my-next-app@0.1.0 start
> next start -p 80 (my domain)
⨯ Invalid project directory provided, no such directory: /root/dashboard/mydomainso dont think --hostname is a thing
@Ray it is
Arboreal antOP
when i added my hostname i got the error "Error: listen EADDRNOTAVAIL: address not available 188.114.96.3:80
" that ip is owned by cloudlfare so idk how i can set the correct hostname
" that ip is owned by cloudlfare so idk how i can set the correct hostname
@Arboreal ant when i added my hostname i got the error "Error: listen EADDRNOTAVAIL: address not available 188.114.96.3:80
" that ip is owned by cloudlfare so idk how i can set the correct hostname
You will need to add this to /etc/hosts
yourdomain.com 127.0.0.1
yourdomain.com 127.0.0.1
@Ray You will need to add this to /etc/hosts
yourdomain.com 127.0.0.1
Arboreal antOP
yh doesnt work, il just use the host header. How do you do it in your projects?
@Arboreal ant yh doesnt work, il just use the host header. How do you do it in your projects?
If you have nginx setup with the domain, it works without any additional setting
Arboreal antOP
okay thanks
@Ray If you have nginx setup with the domain, it works without any additional setting
Arboreal antOP
so tried that, didnt work, i have a nginx setup with this being in sites enabled
server {
listen 80;
server_name mydomain;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
} and request.url still returns localhost@Arboreal ant so tried that, didnt work, i have a nginx setup with this being in sites enabled
server {
listen 80;
server_name mydomain;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
and request.url still returns localhost
Let me check my code and get back to you later
@Ray Let me check my code and get back to you later
Arboreal antOP
any updates?
@Arboreal ant any updates?
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Accel-Buffering "no"; //for streaming support
proxy_pass http://nextjs_upstream/;
proxy_redirect off;
proxy_read_timeout 240s;
}this is the conf
I think this is the problem
proxy_set_header Host $http_host;@Ray I think this is the problem
`proxy_set_header Host $http_host;`
Arboreal antOP
you also have it in your conf file
@Arboreal ant you also have it in your conf file
I use $http_host
you use $host
@Ray I use $http_host
Arboreal antOP
that will make request.url the actual url?
@Arboreal ant that will make request.url the actual url?
that work for me
@Ray that work for me
Arboreal antOP
welp req.url is still localhost:3000....
are you sure you are using req.url in your code? from what ive seen so far req.url just gets your app hostname, it doesnt check the actual request url
yes
@Ray ts
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Accel-Buffering "no"; //for streaming support
proxy_pass http://nextjs_upstream/;
proxy_redirect off;
proxy_read_timeout 240s;
}
this is the conf
Arboreal antOP
you are running nexjts on localhost:3000 and proxying it via nginx?
Arboreal antOP
i dont really wana get the host header just to get the url, is there no other way to get the actual request url instead of the defined hostname? it sounds simple enough
Arboreal antOP
bump
Arboreal antOP
bump
Arboreal antOP
Bump