nginx 502 Bad Gateway on big file uploading - linux

I have a server with apache web server and nginx as proxy. If I'd like to upload a 150MB file, it works without any trouble. But If I try to upload a 350MB file (or larger, I must to upload up to 2GB files) I get nginx 502 Bad Gateway error.
I'm using plesk, and I added these directives to nginx config for testing:
proxy_buffer_size 256k;
proxy_buffers 8 512k;
proxy_busy_buffers_size 512k;
fastcgi_buffers 8 512k;
fastcgi_buffer_size 512k;
And I have increased the client_max_body_size directive too.
I get this error always:
2015/04/19 11:36:09 [error] 31924#0: *43126352 upstream prematurely closed connection while reading response header from upstream, client: x.x.x.x, server: example.com, request: "POST /uptest HTTP/1.1", upstream: "http://x.x.x.x:7080/uptest", host: "example.com", referrer: "http://example.com/uptest"
What should I change?

The FcgidMaxRequestLen or FcgidMaxRequestInMem directives is not large enough, causing the limit to be triggered in many cases (http://httpd.apache.org/mod_fcgid/mod/mod_fcgid.html). FcgidMaxRequestInMem is required to configure due to bug in Apache (bug https://issues.apache.org/bugzilla/show_bug.cgi?id=51747)
Edit fcgid.conffile, which is depending on your linux version could be located in /etc/httpd/conf.d/ or /etc/apache2/mods-available/
Set FcgidMaxRequestLen and FcgidMaxRequestInMem with the same values and then restart Apache.

Related

How to return empty response with Varnish?

When a request come on a Varnish server, I would like to return an empty response or simply close the connection, if the requested server name is not known.
For example from nginx (the backend side of Varnish) I did that:
server {
listen 80 default_server;
listen [::]:80 default_server;
return 444;
}
server {
listen 80;
listen [::]:80;
server_name my.example.org
}
So, when an user/robot come on this nginx server with an address IP or an unknown host, it got: The connection was reset.
How do I?
With this configuration on nginx side and nothing more on Varnish side, if I try to access to the Varnish server with his public IP, I have: Error 503 Backend fetch failed - Backend fetch failed - Guru Meditation.
Perhaps there is a possibility on Varnish side, when response from backend (nginx) is 444, to simply close the connexion.
varnishlog says:
- BereqMethod GET
- BereqURL /
- BereqProtocol HTTP/1.1
...
- BereqHeader X-Varnish: 1540833
- VCL_call BACKEND_FETCH
- VCL_return fetch
- BackendOpen 33 default X.X.X.X 80 X.X.X.X 34862
...
- FetchError HTC eof (-1)
- BackendClose 33 default
...
- BerespProtocol HTTP/1.1
- BerespStatus 503
- BerespReason Backend fetch failed
- BerespHeader Date: Fri, 10 Feb 2023 10:10:48 GMT
- BerespHeader Server: Varnish
- VCL_call BACKEND_ERROR
I want to "process" this error.
If Varnish is hosted on the same machine as your Nginx server, Varnish should be listening on port 80 and Nginx on port 8080.
Once Varnish can reach Nginx, the Backend fetch failed issue will go away.
In Varnish you don't need to configure anything special, whatever Nginx returns, Varnish will handle. However, if you want to handle this in Varnish before Nginx is reached, you could use the following VCL code:
sub vcl_recv {
if(req.http.Host != "my.example.org") {
return(synth(403));
}
}
This assumes that my.example.org is the right Host header. This also assumes that returning a synthetic 403 Forbidden is an acceptable return value.

Docker + Nginx: Websocket returns 404 not found

I'm struggling on implementing a websocket connection between a SSL Server and the client.
Architecture:
Proxy: Nginx
Host: Docker (Swarm)
Webserver: Node.js (express)
Client (Postman, later vue.js)
Nginx settings (app.conf):
server {
listen 443;
listen [::]:443;
client_max_body_size 100M;
server_name search.app search.app.host.ads;
location / {
proxy_pass http://search-service:3020; # docker container
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
search-server (server.js)
async function startServer() {
// variable definition
server = express();
// set http server
let httpServer = http.createServer(server);
// init loaders -> websocket is defined here
loaders(server, httpServer);
}
search-service (websocket.js) (how is the websocket created?)
let wsSearch = new websocket.Server({ server: httpServer, path: "/socket/websocketSearch" });
The websocket is working properly on localhost using this url
ws://localhost:3020/socket/websocketSearch
After deploying on production site, the url will be
wss://search.app.host.ads/socket/websocketSearch
Trying to connect to production websocket using Postman returns following error:
Error: Unexpected server response: 404
Handshake Details
Request URL: https://search.app.host.ads/socket/websocketSearch
Request Method: GET
Status Code: 404 Not Found
Request Headers
Sec-WebSocket-Version: 13
Sec-WebSocket-Key: gzRuxZ2QYTOladlXSenjmw==
Connection: Upgrade
Upgrade: websocket
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
Host: search.app.host.ads
Response Headers
Server: nginx/1.19.4
Date: Tue, 03 Aug 2021 13:07:45 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 161
Connection: keep-alive
X-Powered-By: Express
Access-Control-Allow-Origin: *
Content-Security-Policy: default-src 'none'
X-Content-Type-Options: "nosniff"
Which package do I use to implement websocket?
WS Version 7.5.3
I've followed several instructions like Nginx or related StackOverflow issues. However, I didn't manage to connect to my websocket.
Do you have any idea, where is my fault?
Thanks in advance.
If any further information is needed, I try to provide it.
Best regards

AWS elastic Beanstalk / nginx : connect() failed (111: Connection refused

I got this message
connect() failed (111: Connection refused
Here is my log:
-------------------------------------
/var/log/nginx/error.log
-------------------------------------
2018/10/21 06:16:33 [error] 4282#0: *2 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.4.119, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "hackingdeal-env.qnyexn72ga.ap-northeast-2.elasticbeanstalk.com"
2018/10/21 06:16:33 [error] 4282#0: *2 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.4.119, server: , request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:8081/favicon.ico", host: "hackingdeal-env.qnyexn72ga.ap-northeast-2.elasticbeanstalk.com", referrer: "http://hackingdeal-env.qnyexn72ga.ap-northeast-2.elasticbeanstalk.com/"
I am using nodejs/express Elastic Beanstalk env.
I have one nginx related file in
.ebextensions/nginx/conf.d/proxy.conf
Upper file contains:
client_max_body_size 50M;
Whenever I try to get my webpage I got 502 bad gateway.
What's wrong with my app?
Just recording my incident here just in case it helps someone or my future self. I had a Django application that had SECURE_SSL_REDIRECT set to True. Since I had no load balancers configured to handle HTTPS traffic I was getting a timeout. Setting it to False fixed the issue. Couple of days wasted on that one.
111 connection refused likely means your app isn't running on the server/port combination. Also check that the security group for your app instance (or load balancer) has an inbound rule set to allow traffic from the nginx instance
I was dealing with this error on my NodeJS application (NEXTJS). Posting this here just in case is useful for someone.
My error was thet the deploy command failed at the build step (next build), which means the Node server never restarted. For that reason nginx could not find the server. You can find this kind of errors in the web.stdout.log
I tested my build command locally, fixed the errors and it worked!

nginx node upstream prematurely closed connection while reading response header

I have a nodejs server running behind nginx on elastic beanstalk. I think the following error is because of a promise that isn't being activated?
Does anyone have a better idea about what it is?
2017/03/20 12:18:02 [error] 3503#0: *7363 upstream prematurely closed
connection while reading response header from upstream, client: 111.11.11.111,
server: , request: "POST /api/v1/some/url HTTP/1.1", upstream:
"http://127.0.0.1:8081/api/v1/some/url", host: "some.website.com"
we are also looking to solve that issue. From what I found, it looks like it is because your nodejs does not give a response fast enough. Hope it helps...

Remove request from nginx error logs

How can we remove request url being logged in nginx error logs. For example it looks something like:
2015/09/01 15:26:03 [error] 30547#0: *208725 upstream prematurely closed connection while reading response header from upstream, client: 123.123.50.44, server: test.example.com, request: "GET /v1.3/status.json?...."
is it possible to drop request since it can have PII from the log(if present) so it looks something like:
2015/09/01 15:26:03 [error] 30547#0: *208725 upstream prematurely closed connection while reading response header from upstream, client: 123.123.50.44, server: test.example.com
I was able to configure access logs but couldn't find a way to customize error logs.
Edit:
Is there a way to stop logging only upstream errors?

Resources