strapi / strapi-docker

Install and run your first Strapi project using Docker
https://strapi.io
MIT License
1.17k stars 445 forks source link

Connection Refused When Using Nginx Proxy #251

Closed MehrdadKhnzd closed 3 years ago

MehrdadKhnzd commented 4 years ago

Describe the bug I followed the instruction here: https://strapi.io/documentation/v3.x/deployment/nginx-proxy.html But Nginx gives me 502 - Connection Refused.

Expected behavior It should work!

Code snippets It's just like the link above, except I don't have https.

System I'm using strapi/strapi docker image, the latest one.

Additional context I also tried adding proxy: true to the server config, which not worked.

derrickmehaffy commented 4 years ago

Please provide more information as to your setup and configuration. Error 502 means Nginx can't connect to your Strapi server. Can you provide some of the Nginx logs, and check the ports currently listening?

If on Linux, you can use netstat -plnt to check ports

MehrdadKhnzd commented 4 years ago

My Strapi server config:

module.exports = ({ env }) => ({
  host: env('HOST', '0.0.0.0'),
  port: env.int('PORT', 1337),
  admin: {
    auth: {
      secret: env('ADMIN_JWT_SECRET', 'some-secret-here'),
    },
  },
  url: env('PUBLIC_URL', 'http://localhost:1337'),
  proxy: env.bool('PROXY', false)
});

I provide PUBLIC_URL as http://sub.domain.app/api and PROXY as true.

And my Nginx config:

server {
  listen         80;
  listen         [::]:80;
  server_name    sub.domain.app;

  location /api/ {
    rewrite ^/api/(.*)$ /$1 break;
    proxy_pass http://strapi;
    proxy_http_version 1.1;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Server $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_set_header Host $http_host;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    proxy_pass_request_headers on;
  }

  location / {
    root           /var/www/app;
    index          index.html;
    try_files $uri /200.html = 404;
  }
}

And my upstream in conf.d:

upstream strapi {
    server 127.0.0.1:1337;
}

And about your question about listening ports, here's the relevant line:

tcp6 | 0 | 0 | :::1337 | :::* | LISTEN | -

I can see that the Strapi server is up and running, and after starting it up, it says that my admin panel will be accessible at http://sub.domain.app/api/admin, but immediately after, it logs a warning which shows that it can't connect to admin (which was misspelled as "adm"). And when I access that address from the browser, the Nginx completely understands and correctly tries to reach http://127.0.0.1:1337/admin, but it says that the connection is refused and gives me error 502. And as I mentioned, I also tried with and without the proxy option in the server config. Thanks for your help! @derrickmehaffy

MehrdadKhnzd commented 4 years ago

Just to mention, my Nginx is also a container.

derrickmehaffy commented 4 years ago

When you pass in the PUBLIC_URL that also needs to be passed in when the admin panel is built. That specific variable is not dynamic and is hard coded into the build. Does your docker container always rebuild the admin?

MehrdadKhnzd commented 4 years ago

I use the strapi/strapi:alpine image with the following Docker Compose config:

  api:
    container_name: api
    image: strapi/strapi:alpine
    restart: always
    environment:
      # Some DB Config!
      - NODE_ENV=production
      - "PUBLIC_URL=http://sub.domain.app/api"
      - PROXY=true
    ports:
      - 1337:1337
    volumes:
      - ../api:/srv/app
    depends_on:
      - db

And that's all I have.

derrickmehaffy commented 4 years ago

It would appear the Admin is not being rebuilt then based on: https://github.com/strapi/strapi-docker/blob/master/strapi/docker-entrypoint.sh

That would be an issue to post on https://github.com/strapi/strapi-docker though.

MehrdadKhnzd commented 4 years ago

@derrickmehaffy Thanks, I opened an issue there. But what can I do for now? I tried to build it as a custom docker container but it didn't work completely. Should I wait for a response in that repository?

derrickmehaffy commented 4 years ago

In your docker-compose file where it says RUN npm install

Below that can you add RUN npm run build

MehrdadKhnzd commented 4 years ago

I tried a custom Dockerfile but it didn't work. It's exactly the same as the strapi/strapi container. Here's the output:

api          |
api          |  Project information
api          |
api          | ┌────────────────────┬──────────────────────────────────────────────────┐
api          | │ Time               │ Sat Oct 03 2020 13:15:08 GMT+0000 (Coordinated … │
api          | │ Launched in        │ 43927 ms                                         │
api          | │ Environment        │ production                                       │
api          | │ Process PID        │ 27                                               │
api          | │ Version            │ 3.1.5 (node v12.18.4)                            │
api          | │ Edition            │ Community                                        │
api          | └────────────────────┴──────────────────────────────────────────────────┘
api          |
api          |  Actions available
api          |
api          | One more thing...
api          | Create your first administrator � by going to the administration panel at:
api          |
api          | ┌────────────────────────────────────┐
api          | │ http://sub.domain.app/api/admin │
api          | └────────────────────────────────────┘
api          |
api          | warn ⚠️  The admin panel is unavailable... Impossible to open it in the browser.

And here's my Dockerfile:

FROM node:lts-alpine
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn
COPY . .
RUN node --max-old-space-size=4096 node_modules/strapi/bin/strapi build
EXPOSE 1337
CMD ["yarn", "start"]

And here's the Nginx log:

[error] 19#19: *34 connect() failed (111: Connection refused) while connecting to upstream, client: some-ip, server: sub.domain.app, request: "GET /api/ HTTP/1.1", upstream: "http://127.0.0.1:1337/", host: "sub.domain.app"

And my problem still exists, just as before. I don't think it's related to strapi/strapi docker image. Now I don't use it at all and everything's just the same.

alexandrebodin commented 4 years ago

Hello, you need to add the yarn build step in your Dockerfile

derrickmehaffy commented 4 years ago

Hello, you need to add the yarn build step in your Dockerfile

Something similar to this:

FROM node:lts-alpine
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn
COPY . .
RUN yarn build
EXPOSE 1337
CMD ["yarn", "start"]
MehrdadKhnzd commented 3 years ago

Hey there, sorry for the delay. I found out the problem some days ago but I was very busy I couldn't post it here. The problem was here:

upstream strapi {
    server 127.0.0.1:1337;
}

As I said, I used Nginx in a container, and by doing that, I was referencing the Nginx container itself when I wrote 127.0.0.1. I changed it to the container name and now it works! Although I still can't get the admin panel and the documentation from the subpath, which may be another thread or issue, at least the API works itself, and I will open another issue for that if needed. Thanks again!

maggie44 commented 3 years ago

It would appear the Admin is not being rebuilt then based on: https://github.com/strapi/strapi-docker/blob/master/strapi/docker-entrypoint.sh

That would be an issue to post on https://github.com/strapi/strapi-docker though.

I just experienced this issue too, and lost hours trying to work out what the problem is. The yarn build inside the container fixed it, but I agree, the Admin should be rebuilt when you provide a PUBLIC_URL, or the PUBLIC_URL should be a variable read from a config file.