nginx: increase timeout to prevent 404 not found error? - nginx

I have a Django server running Gunicorn, and in front of that I have nginx. I serve static files directly from nginx, and pass other things through to Gunicorn.
I have some slow-running back-end queries, and I'm finding that nginx is quite often timing out before they return - so I see a 404 page.
Is there a way I can increase the timeout level?
This is my nginx conf file:
server {
listen 443;
client_max_body_size 4G;
access_log /webapps/myapp/logs/nginx-access.log;
error_log /webapps/myapp/logs/nginx-error.log;
location /media/ {
alias /webapps/myapp/myapp/media/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://hello_app_server;
break;
}
}
I think perhaps I need proxy_read_timeout, but I'm not sure from the docs.

Try
proxy_read_timeout 120s;
Put that inside your proxy section.
The default is apparently 60s so try doubling and go from there.
Not too confident about it but i had something similar with a timeout in mysql today on a server at work and doubling that worked. Worth a try and hope it helps.

Related

Plex behind nginx reverse proxy with get requests to different route

I am trying to link all the services on my network together with nginx. One of those services is Plex. The nginx server is running inside docker on 192.168.1.150:80. The plex server is running on 192.168.1.149:32400. I also have a homer instance running on 192.168.1.148:80 I have a working config (see below), but I want to change something that I do not know how.
Nginx.conf:
user nginx;
worker_processes 5;
events {
worker_connections 2048;
}
http {
server {
location / {
proxy_pass http://192.168.1.148:80;
}
location /plex {
proxy_pass http://192.168.1.149:32400/web;
}
location /web {
proxy_pass http://192.168.1.149:32400/web;
}
}
}
As you can see, because plex requests resources from subdomain '/web', I have to add the proxy_pass for /web to go to plex as well. This is far from ideal when I want to use the subdomain /web for something else. The plex's index.html requests some script from /web/.... Is there some way to have this request go to /plex/web, so that I can catch it in that subdomain and not in the global one. This way I can use /web for something else.
Thanks in advance
ExellentCoin
Not sure if you found a solution but came across your post researching Plex configs and had the same question. This is what I came up with and works:
location /plex/ {
proxy_pass http://192.168.68.102:32400/;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
proxy_ssl_verify off;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 86400;
}
if ($http_referer ~ /plex/) {
rewrite ^/web/(.*) /plex/web/$1? redirect;
}

How to redirect all incoming http request to a specific url in nginx config file?

I know , I know. This question is asked too many times and I've researched it alot as well. But every solution on internet lead me to dead end.
I want to redirect all the incoming http-request to a specific url/domain.
For example if someone type - www.test.com or simply test.com in browser's url-bar, it should redirect the user to http://test.com/home .
This is what I've been trying to achieve from last 3 days, not sure what I'm doing wrong. This is my server-block.
server {
listen 80;
server_name test.com;
port_in_redirect off;
# server_name_in_redirect off;
client_max_body_size 20M;
location / {
proxy_set_header Host $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_redirect off;
proxy_pass http://localhost:8000/ ;
proxy_read_timeout 90;
return 301 http://test.com/home;
}
}
This above configuration is giving me error - too many redirection on browser when I'm trying to access the website.Also removing return statement giving me page not found error and not changing/redirecting the url to http.test.com/home.
PS - I'm running another different website as well on this same server on port 443(https) & that is working absolutely well. I'm running a Spring-boot application.
A help is highly appreciated.
You write too many codes related to redirection, that's why showing this error.
Change your server block like below.
server {
listen 80;
server_name test.com www.test.com;
client_max_body_size 20M;
#return 301 http://test.com/home$request_uri;
}
location ~ ^/(?!home) {
return 301 http://test.com/home$request_uri;
}
location /home {
root /var/www/test.com/html/;
index index.html index.htm;
}

Tornado app in multiple nginx locations

I have 2 tornado applications and I am trying to use nginx as a proxy for them, but I need those applications to be served in the same address but different locations (Access app1 with URL http://myserver/app1, and app2 with URL http://myserver/app2).
My nginx configuration file /etc/nginx/conf.d/myserver.conf:
upstream app1 {
server 127.0.0.1:8081;
server 127.0.0.1:8082;
}
upstream app2 {
server 127.0.0.1:9081;
server 127.0.0.1:9082;
}
server {
listen 80;
access_log /var/log/nginx/myserver.access.log;
error_log /var/log/nginx/myserver.error.log;
location app1/static {
root /path/to/app1/;
if ($query_string) {
expires max;
}
}
location app2/static {
root /path/to/app2/;
if ($query_string) {
expires max;
}
}
location /app1/ {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_pass http://app1/;
}
location /app2/ {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_pass http://app2/;
}
}
When I access, for instance, app1 via tornado it works fine:
Via tornado: http://myserver:8081/ redirects to login http://myserver:8081/auth/login. Good.
Via nginx: http://myserver/app1 redirects to http://myserver/auth/login (it should redirect to http://myserver/app1/auth/login).
What is the correct nginx configuration to make it work?
This is controlled by the proxy_redirects setting. You've turned it off, so when the tornado server redirects to /auth/login that gets passed through as-is. You need to either make the tornado server aware of its urls as seen by the outside world (i.e. include /app1/ in all the routes and redirects even internally) or turn on proxy_redirects to have nginx remap them. I recommend the former, since proxy_redirects only works for redirects and you'll usually run into similar issues in other places (urls for static content, for submission, etc).

Nginx - Forward root-level URL (eg, favicon.ico) to Amazon S3?

I'm trying to configure my nginx server to serve root level assets, like www.domain.com/favicon.ico, from S3.
I think, but am not sure, that I'm supposed to proxy_pass to accomplish this. I'm also stuck on the location regex:
server {
listen 80;
server_name *.domain.com
# This is where I'm trying to catch URLs like /favicon.ico
location ~* /*\.(xml|txt|png|ico)$ { # wrong
proxy_pass http://<s3_bucket>?? # wrong
}
# Everything else goes to gunicorn/Django.
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
It's right to do this using proxy_pass. If you have problem you can set error log to debug level to trace the problem. From the trace log, you should focus on two points:
Whether /*\.(xml|txt|png|ico)$ match your requests?
Whether the url being passed is right and access successfully?

nginx subdomain to directory , too many redirect , why?

this is my config:
server {
listen 80;
server_name ~^(?<sb>.+)\.a\.b\.c\.com$;
access_log /data/logs/nginx/tas.access.log main;
location / {
proxy_intercept_errors on;
proxy_pass http://b.c/a/$sb/;
proxy_set_header Host $host;
proxy_redirect off;
}
}
and browser report to many redirects.
If, as you say, you want to proxy to localhost:8082, you need to say so in the proxy_pass line:
server {
listen 80;
server_name ~^(?<sb>.+)\.a\.b\.c\.com$;
access_log /data/logs/nginx/tas.access.log main;
location / {
proxy_intercept_errors on;
proxy_pass http://localhost:8082/a/$sb/;
proxy_set_header Host $host;
proxy_redirect off;
}
}
Without all of the information, it's hard to guess what's going on. Based on the comments, my guess is that you are using virtual hosting so that the upstream site is also served by the same nginx. So this line is the problem:
proxy_set_header Host $host;
The nginx variable $host is pointing to the current Host header (which matches the server_name). So if you set the same host header for the upstream again, then nginx will find the same location block above because nginx relies on the Host header to find the proper server. Thus the redirect loop.
Set
proxy_set_header Host your_upstream_server_name
will fix it then.

Resources