ngxtop - real-time metrics for nginx server (and others)
ngxtop parses your nginx access log and outputs useful, top-like, metrics of your nginx server. So you can tell what is happening with your server in real-time. ngxtop tries to determine the correct location and format of nginx access log file by default, so you can just run ngxtop and having a close look at all requests coming to your nginx server. But it does not limit you to nginx and the default top view. ngxtop is flexible enough for you to configure and change most of its behaviours. You can query for different things, specify your log and format, even parse remote Apache common access log with ease. See sample usages below for some ideas about what you can do with it.
Installation:
pip install ngxtop
It is easy as pie, you just need to run it and look at the results:
nxtop
It will reports for total requests served and total bytes sent to client and will report requests based on their status code. At the pictures in the next post you can see sample usages.
#linux #nginx #top #nxtop #web_server
504 Gateway timeout
It is a known issue to many people even those who are not in the programming field. 504 timeout happens when a response for a request has taken longer than expected. There are times that you know and are sure that you need to increase this time. For example if users export a huge excel file as a report. In nginx you can increase this time to what seems to be appropriate from programmer point of view.
In
nginx.conf
usually in /etc/nginx/
do as follow:proxy_connect_timeout 600s;
proxy_send_timeout 600s;
proxy_read_timeout 600s;
send_timeout 600s;
More info: https://nginx.org/en/docs/http/ngx_http_proxy_module.html
#504 #gateway_timeout #timeout #nginx #proxy_read_timeout #proxy_send_timeout
How do you give maintenance for your infrastructure when a new BIG deployment comes in?
You can use
Inside of your
What happens here is that we first check for a file named
Outside of location we need to server
And with an exact match for the file (`location =`) we serve that static page from root of
The note about this code is that when your file is named
#linux #nginx #maintenance #maintenance_mode #location #error_page
You can use
nginX
to display a nice page about your maintenance. First of all create your fancy landing page for your maintenance in html format with the name of maintenance_off.html
.Inside of your
server
block in nginX configuration we do like below:server {
...
location / {
if (-f /webapps/your_app/maintenance_on.html) {
return 503;
}
...
}
# Error pages.
error_page 503 /maintenance_on.html;
location = /maintenance_on.html {
root /webapps/your_app/;
}
...
}
What happens here is that we first check for a file named
maintenance_on.html
, if it's present we return 503 Server error
. This error code is returned when server is not available. This code is inside of location
section of root.Outside of location we need to server
/maintenance_on.html
for error 503:error_page 503 /maintenance_on.html;
And with an exact match for the file (`location =`) we serve that static page from root of
/webapps/your_app/
.The note about this code is that when your file is named
maintenance_off.html
maintenance will be ignored and when we rename the file to maintenance_on.html
then error 503 is returned.#linux #nginx #maintenance #maintenance_mode #location #error_page
There are many other ways to put a website in maintenance mode, like to allow specific ip addresses to see the website but others see the maintenance mode page:
This is it. If you don't know what the above code do, then SEARCH. :)
#nginx #linux #maintenance #maintenance_mode #503
server {
..
set $maintenance on;
if ($remote_addr ~ (34.34.133.12|53.13.53.12)) {
set $maintenance off;
}
if ($maintenance = on) {
return 503;
}
location /maintenance {
}
error_page 503 @maintenance;
location @maintenance {
root /var/www/html/maintenance;
rewrite ^(.*)$ /index.html break;
}
..
}
This is it. If you don't know what the above code do, then SEARCH. :)
#nginx #linux #maintenance #maintenance_mode #503
In case you want to serve static files in your website in nginX, you can add a new
This is it, whether it is a uwsgi proxy, fpm, etc.
#web_server #nginx #static #location #serve
location
directive to your server block that corresponds to your website:server {
# your rest of codes in server block...
location / {
location ~ \.(css|ico|jpg|png) {
root /etc/nginx/www/your_site/statics;
}
# your rest of codes...
}
}
This is it, whether it is a uwsgi proxy, fpm, etc.
#web_server #nginx #static #location #serve
When you
But there is tiny tip here that needs to be told. If you want to pass parameter to the destination link, which in here is
The variable
#nginx #web_server #redirect #302 #is_args #args
redirect
in nginX
you would use one of 302, 301 code like the below code:location = /singup {
return 302 https://docs.google.com/forms;
}
But there is tiny tip here that needs to be told. If you want to pass parameter to the destination link, which in here is
https:// docs.google.com/forms
it wont work. String parameters are being held in $args
varaible in nginX
so you need to pass this variable like the following code:location = /singup {
return 302 https://docs.google.com/forms$is_args$args;
}
The variable
$is_args
value will be set to "?" if a request line has arguments, or an empty string otherwise.#nginx #web_server #redirect #302 #is_args #args
By default when you install
nginX
on Linux
a logrotate config file will be created in /etc/logrotate.d/nginx
. Sometimes you may see that af