Next.js Discord

Discord Forum

Docker, Plesk, Next-Auth Deploying Error

Unanswered
MeKa posted this in #help-forum
Open in Discord
I have 1 domain name, 1 server and next.js project. Plesk Panel is installed on the server. The domain name is linked to my address. I build the project with Docker and upload the image. I have information defined in the .ENV file, such as AUTH_URL & AUTH_SECRET, but the problem is that docker automatically assigns this IP address.

  ▲ Next.js 14.2.3
   - Local: http://6943f96c01cd:3000
   - Network: http://172.17.0.2:3000


but since AUTH_URL = https://domainname.com in my .env file, it gives an "Internal Server Error" error. How can I direct these "Network and Local" IP addresses on Docker to my domain?

107 Replies

Dockerfile

FROM node:20-alpine AS deps

RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json ./
RUN npm install -g npm@10.6.0
RUN npm install

####################################
FROM node:20-alpine AS build
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .

RUN npx prisma generate
RUN npm run build

####################################
FROM node:20-alpine AS deploy

WORKDIR /app

ENV NODE_ENV production

COPY --from=build /app/public ./public
COPY --from=build /app/package.json ./package.json
COPY --from=build /app/.next/standalone ./
COPY --from=build /app/.next/static ./.next/static

EXPOSE 3000

ENV PORT 3000

CMD ["node", "server.js"]
Has anyone ever used Docker on a server or can anyone help?
Also try: in the last line in the hostname, try setting the hostname to whatever your domain is.
Yes try it
@Clown Yes try it
⨯ Failed to start server
Error: getaddrinfo ENOTFOUND https://domainname.com
at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:107:26) {
errno: -3008,
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'https://domainname.com'
}
@Clown Yes try it
I deleted "https://" and left only the domain name. The SERVER IP address was automatically assigned there and now it gives a different error.

⨯ Failed to start server
Error: listen EADDRNOTAVAIL: address not available SERVER IP address:3000
at Server.setupListenHandle [as _listen2] (node:net:1800:21)
at listenInCluster (node:net:1865:12)
at GetAddrInfoReqWrap.doListen [as callback] (node:net:2014:7)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:109:8) {
code: 'EADDRNOTAVAIL',
errno: -99,
syscall: 'listen',
address: 'SERVER IP address',
port: 3000
}
Does setting it as "0.0.0.0" do something?
I'm trying right now.
If your project needs to listen to a specific port or hostname, you can define PORT or HOSTNAME environment variables before running server.js. For example, run PORT=8080 HOSTNAME=0.0.0.0 node server.js to start the server on http://0.0.0.0:8080
@Clown Does setting it as "0.0.0.0" do something?
▲ Next.js 14.2.3
- Local: http://localhost:3000
- Network: http://0.0.0.0:3000

✓ Starting...
✓ Ready in 95ms
@Clown > If your project needs to listen to a specific port or hostname, you can define PORT or HOSTNAME environment variables before running server.js. For example, run PORT=8080 HOSTNAME=0.0.0.0 node server.js to start the server on http://0.0.0.0:8080
I can already run the site, no problem. The important thing is that the AUTH_URL matches and works, and now it gives the "Internal Server Error" error again. But I'll try it on localhost rather than the domain name in the .env file, let's see.
@Clown > If your project needs to listen to a specific port or hostname, you can define PORT or HOSTNAME environment variables before running server.js. For example, run PORT=8080 HOSTNAME=0.0.0.0 node server.js to start the server on http://0.0.0.0:8080
▲ Next.js 14.2.3
- Local: http://localhost:3000
- Network: http://0.0.0.0:3000

✓ Starting...
✓ Ready in 99ms
[auth][warn][env-url-basepath-redundant] Read more: https://warnings.authjs.dev#env-url-basepath-redundant
[auth][error] UnknownAction: Cannot parse action at 3000/3000/session. Read more at https://errors.authjs.dev#unknownaction
at /app/.next/server/chunks/3986.js:6:58639
at tQ (/app/.next/server/chunks/3986.js:6:58706)
at iF (/app/.next/server/chunks/3986.js:382:46691)
at iG (/app/.next/server/chunks/3986.js:382:52141)
at /app/.next/server/chunks/3986.js:382:53113
at A (/app/.next/server/chunks/8138.js:1:21399)
Error: URL is malformed "null/tr/login". Please use only absolute URLs - https://nextjs.org/docs/messages/middleware-relative-urls
at o (/app/.next/server/middleware.js:431:9380)
at new l (/app/.next/server/middleware.js:431:6064)
... 5 lines matching cause stack trace ...
at AsyncLocalStorage.run (node:async_hooks:338:14)
at Object.wrap (/app/.next/server/middleware.js:2:13282)
at /app/.next/server/middleware.js:2:15864 {
[cause]: TypeError [ERR_INVALID_URL]: Invalid URL
at new NodeError (node:internal/errors:405:5)
at new URL (node:internal/url:676:13)
at o (/app/.next/server/middleware.js:431:9345)
at new l (/app/.next/server/middleware.js:431:6064)
at oJ (/app/.next/server/middleware.js:378:51010)
at o6 (/app/.next/server/middleware.js:378:55861)
at r (/app/.next/server/middleware.js:378:55547)
at Object.ay [as handler] (/app/.next/server/middleware.js:378:62992)
at /app/.next/server/middleware.js:2:16051
at AsyncLocalStorage.run (node:async_hooks:338:14) {
input: 'null/tr/login',
code: 'ERR_INVALID_URL'
}
}
.env

AUTH_URL=localhost:3000
API_URL=localhost:3000/api
NEXT_PUBLIC_API_URL=localhost:3000/api
SITE_URL=localhost:3000
NEXT_PUBLIC_SITE_URL=localhost:3000
Maybe try 0.0.0.0:3000 for the auth url too?
@Clown Maybe try 0.0.0.0:3000 for the auth url too?
I tried but "npm run build" gives an error and I cannot get any output, or I get an error when I enter a different ip number (server).
@Clown Maybe try 0.0.0.0:3000 for the auth url too?
Without Plesk, can we point the domain and NS addresses to a server where nothing is installed? then can we run the project without needing Docker? Can you help me if you have any information? Because I'm so tired of this situation. I've been trying for 2 weeks. There is no method I haven't tried.
Its a pretty simple and straight forward way
@Clown When not using Plesk and Docker you would normally just run your nextjs server and then use a nginx server or something similar
Yes, I reset the server and I am trying to do it with nginx but there is the same error here.
Maybe you would just use localhost:3000 when using this way?
@Clown Maybe you would just use localhost:3000 when using this way?
Sir, I have a domain name and this next.js, why is it so problematic? Even the administrators here are not helping. It has been 2 weeks and I will finally delete next.js. OK, I may have made a wrong configuration. but there is no one to help. They know that when you write it on Off Topic, it will come immediately, but no, open the topic here and do it this way.
@averydelusionalperson Come, please.
okay, this is a fresh server with nginx right?
Yes, but I installed it myself according to the documentation.
which documentation
@averydelusionalperson which *documentation*
I wrote the site name specifically.
okay, so what is the issue you are facing?
Everything works and there is no problem, but I cannot access the "/login", "/register" sections. The reason is because it does not match the "AUTH_URL" name in the .env file.
@averydelusionalperson okay, so what is the issue you are facing?
I can't view the logs right now.
But if I install Plesk and publish with Docker, I can monitor the logs from the Docker Console. But I struggled a lot with docker and I couldn't redirect it to the domain.
@MeKa I can't view the logs right now.
why? are you on mobile
anyways, what's the NEXTAUTH_URL in .env?
@averydelusionalperson anyways, what's the `NEXTAUTH_URL` in .env?
The site address is written: "AUTH_URL=https://domainname.com"
the link you sent to me?
the domain name is correct right?
also, it's NEXTAUTH_URL btw
nvm
are you using next-auth v5 ?
okay, then it's AUTH_URL
needed confirmation
okay, can you show the logs?
logs are kinda important to solve a bug
Let me try to set it up for a minute.
I couldn't adjust 😦
no logs?
if you're using pm2
it has logs right?
@MeKa Sir, I have a domain name and this next.js, why is it so problematic? Even the administrators here are not helping. It has been 2 weeks and I will finally delete next.js. OK, I may have made a wrong configuration. but there is no one to help. They know that when you write it on Off Topic, it will come immediately, but no, open the topic here and do it this way.
Because the server exists for the community to help one another. The actual nextjs team gains nothing out of this.

My reasoning behind saying that you should just use localhost:3000 as the url in your .env is simply because the app itself is running on localhost:3000. Your nginx server is what is reverse proxing your "SERVER IP" to the nextjs server(running on localhost:3000).

DOMAIN (DNS Resolution) -> SERVER_IP/ -> localhost:3000(reverse proxy)
wouldn't next redirect to the localhost:3000 on the browser then?
@averydelusionalperson wouldn't next redirect to the `localhost:3000` on the browser then?
Toyger
when you configure reverse proxy with nginx it rewrites host for final destination
proxy_set_header Host $host;

which nextjs uses as current host.
hmm, noice. pretty new to nginx, good to know
I can't look at it because I'm working at a different job right now. I will come in half an hour or 1 hour. I'm very new to Nginx. I installed according to the documentation. And I don't think Logs can happen. You can take a look at the documentation for this, if you don't mind. The problem is that the project works, but the "/login/", "/register" url addresses give an "interval server error" error. I cannot access the Administration Panel because it gives this error.
And I don't think Logs can happen.
would be very hard to debug then
pm2 logs should give you logs tho
server {
        listen 80;
        server_name domainnameexample.com; # !!! - change to your domain name 
      gzip on;
        gzip_proxied any;
        gzip_types application/javascript application/x-javascript text/css text/javascript;
        gzip_comp_level 5;
        gzip_buffers 16 8k;
        gzip_min_length 256;

    location /_next/static/ {
                alias /var/www/domainnameexample.com/.next/static/; # !!! - change to your app name
                expires 365d;
                access_log off;
        }

    location / {
                proxy_pass http://127.0.0.1:3000; # !!! - change to your app port
                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;
        }
}
@averydelusionalperson > And I don't think Logs can happen. would be very hard to debug then
How can I look at it? If you are available, I will turn on the computer, you can connect and look at it as you wish. In the meantime, let me look at the businesses I belong to. Don't let the boss get angry.
@averydelusionalperson pm2 logs should give you logs tho
I'm at the computer right now.
@MeKa server { listen 80; server_name domainnameexample.com; # !!! - change to your domain name gzip on; gzip_proxied any; gzip_types application/javascript application/x-javascript text/css text/javascript; gzip_comp_level 5; gzip_buffers 16 8k; gzip_min_length 256; location /_next/static/ { alias /var/www/domainnameexample.com/.next/static/; # !!! - change to your app name expires 365d; access_log off; } location / { proxy_pass http://127.0.0.1:3000; # !!! - change to your app port 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; } }
Toyger
if you are using docker this config isn't right
for example
location /_next/static/ {
                alias /var/www/domainnameexample.com/.next/static/; # !!! - change to your app name
                expires 365d;
                access_log off;
        }

nginx don't have access inside docker volume, and with this path it's isnt docker.
you have to use nextjs docker config, and use simple reverse proxy, at least to make it work, and then you can add cacshed etc.....
@Toyger what you see right now in a browser when you open domain? what `pm2 logs` show?
When I set the .env to "http://localhost:3000", the site worked, that is, "/login", but it redirected me to the "http://localhost:3000/" url address where I wanted to log in, not "https://domainname.com".
and it's more interesting
what exactly variable you set to this ?
Once a user logs in, it should redirect to "https://domainname.com". Not to "http://localhost:3000". The first logically correct example
When I type the real address, I cannot log in because I get the "Internal Server Error" error.
Toyger
why do you need all of this?
you only need AUTH_URL and it should be set to your real domain
@Toyger why do you need all of this? you only need AUTH_URL and it should be set to your real domain
Okay, but I'm using the others in different projects, they are unnecessary, yes, the only important thing right now is AUTH_URL. But when I type the real domain name, I get an "Interval Server Error" error.
@Toyger when you get internal server error check `pm2 logs` it will show what's wrong
Wait a minute, I immediately change the AUTH_URL and try that.
@Toyger when you get internal server error check `pm2 logs` it will show what's wrong
pm2 start npm --name "domainname.com" -- start

and pm2 logs

Failed to proxy https://domainname.com/en/login Error: connect ECONNREFUSED ServerİpNumber:443
0|jeten | at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1605:16)
0|jeten | at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
0|jeten | errno: -111,
0|jeten | code: 'ECONNREFUSED',
0|jeten | syscall: 'connect',
0|jeten | address: 'ServerİpNumber',
0|jeten | port: 443
0|jeten | }
0|jeten | Error: connect ECONNREFUSED ServerİpNumber:443
0|jeten | at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1605:16)
0|jeten | at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
0|jeten | errno: -111,
0|jeten | code: 'ECONNREFUSED',
0|jeten | syscall: 'connect',
0|jeten | address: 'ServerİpNumber',
0|jeten | port: 443
0|jeten | }
/etc/nginx/sites-available# nano domainname.com

server {
        server_name domainname.com;
        gzip on;
        gzip_proxied any;
        gzip_types application/javascript application/x-javascript text/css text/javascript;
        gzip_comp_level 5;
        gzip_buffers 16 8k;
        gzip_min_length 256;

    location /_next/static/ {
                alias /var/www/domainname.com/.next/static/;
                expires 365d;
                access_log off;
        }

    location / {
                proxy_pass http://127.0.0.1:3000; # !!! - change to your app port
                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;
        }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/domainname.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/domainname.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 = domainname.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


        listen 80;
        server_name domainname.com;
    return 404; # managed by Certbot
}`
@MeKa AUTH_URL=https://domainname.com
Toyger
try to remove slash at the end
@Toyger try to remove slash at the end
No, it doesn't exist, it just adds it automatically on Discord.
@MeKa No, it doesn't exist, it just adds it automatically on Discord.
Toyger
ok, try to set
AUTH_URL_INTERNAL=http://localhost:3000
and comment AUTH_URL
@MeKa I don't understand
Toyger
your env file should be
#AUTH_URL=https://domainname.com
AUTH_URL_INTERNAL=http://localhost:3000
at least lines that needed for nextauth
@MeKa Should I disable AUTH_URL?
Toyger
yes
@Toyger yes
Why is this "AUTH_URL_INTERNAL=http://localhost:3000"?
@MeKa Why is this "AUTH_URL_INTERNAL=http://localhost:3000"?
Toyger
because docs say so
@Toyger because docs say so
No, it is still the same, but the "/login" page opens, but the redirection is made to "http://localhost:3000/".
@MeKa No, it is still the same, but the "/login" page opens, but the redirection is made to "http://localhost:3000/".
Toyger
I tried to deploy it, and only error I get was trusthost
I added it to auth.ts
trustHost: true,

and it show page fine.

I didn't even defined AUTH_URL as they suggest in docs.
Toyger
oh ok, it showed wrong links for auth, but I added AUTH_URL and it redirects.
I didn't defined oauth keys.
https://test.hstack.top/
@MeKa If you are free tonight, can you connect my pc for five mins please?
Toyger
neither free, neither don't want to connect to anyone's computer.
I am tested your tutorial and it works ok, try to create blank nextjs app and configure next-auth again.
@Toyger neither free, neither don't want to connect to anyone's computer. I am tested your tutorial and it works ok, try to create blank nextjs app and configure next-auth again.
The important thing is to run this project I made for my friend because it is not a small project.
auth.ts

import NextAuth from "next-auth";
import { PrismaAdapter } from "@auth/prisma-adapter";
import prisma from "prismadb";
import authConfig from "auth.config";

export const { handlers, auth, signIn, signOut } = NextAuth({
  secret: process.env.AUTH_SECRET,
  debug: process.env.NODE_ENV !== "production",
  adapter: PrismaAdapter(prisma),
  trustHost: true,
  ...authConfig,
});
@MeKa How did you configure the server nginx?
Toyger
I used same config from same tutorial as you.
@Toyger I used same config from same tutorial as you.
If everything is the same, why am I having problems with error redirection?
@MeKa If everything is the same, why am I having problems with error redirection?
Toyger
maybe your domain configured in different way, do you use cloudlfare proxy?
@Toyger maybe your domain configured in different way, do you use cloudlfare proxy?
I bought both domain name and server from hostinger. I directed the domain name A record to the ip number. I renamed the CNAME address www and directed it to the domain name. There is nothing extra different.
@Toyger maybe your domain configured in different way, do you use cloudlfare proxy?
I will say something; I am using the "next-intl" package for language translation. Could this be the reason?
@MeKa I will say something; I am using the "next-intl" package for language translation. Could this be the reason?
Toyger
as I said you better try with blank project, so you'll have something as proof that it works at least somehow, if blank will be fine then you move your project and remove unnecessary things until it will work to understand what exactly make this error, intl can be a problem, but I never tried it with nextauth especially v5 can be buggy