My dockerfile looks somewhat like this: -- simplified
FROM ubuntu:22.10
# Create my_user ...
# Copy Nginx configuration files
COPY ./nginx.conf /etc/nginx/nginx.conf
COPY ./proxy.conf /etc/nginx/conf.d/proxy.conf
WORKDIR /app
RUN npm install \
&& npm run build
WORKDIR /app/frontend
RUN npm install \
&& npm run build
RUN sudo cp -a ./dist/. /var/www/html
WORKDIR /app
CMD ["sh", "-c", "nginx -g 'daemon off;' & npm run start:prod"]
This is my proxy.conf
upstream backend {
server localhost:3000;
}
server {
listen 8080;
server_name _;
root /var/www/html;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
location ~ \.(css|js)$ {
add_header Content-Type text/plain;
try_files $uri =404;
}
location /api {
proxy_pass http://backend/api;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
This is my nginx.conf
user my_user;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# Other event configurations if needed
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
sendfile on;
include /etc/nginx/conf.d/*.conf;
}
Locally this works like a charm but once deployed in cloud run and trying to access a path under /api like my_domain.bla/api/here/we/are I get a log from nginx saying:
[error] 10#10: *9 connect() failed (111: Connection refused) while connecting to upstream, client: #certain_IP, server: _, request: "GET /api/here/we/are HTTP/1.1", upstream: "http://127.0.0.1:3000/api/here/we/are", host: "my_domain.bla""
Nginx is listening on port 8080 and my backend app is running on port 3000 (I see clear logs coming from the container confirming the app is running fine and listening on the correct port)
I expect the proxy_pass to work on cloud run as well as it does locally. Does anybody understand the difference between cloud run and locally? My first thought is that nginx might be trying to route the request to the host and not to the container internally. The backend port is not exposed so it would explain the connection refused but how do I fix that?
Your Nginx application requires more CPU time than your current Cloud Run configuration provides. This guide on the lifecycle of a container will show how CPU time is allocated.
Enable CPU always allocated
for your Cloud Run instance.
There are additional costs when the CPU is always allocated. Review this document for more details.