1. HTTP -> HTTPS
To automate detection of HTTP vs HTTPS, I would use 2 separate server
blocks:
server {
listen 80 default_server;
listen [::]:80 default_server;
# Redirect to HTTPS
}
server {
listen 443 default_server ssl;
listen [::]:443 default_server ssl;
# Serve content
}
Redirect will be done through the use of return 301
rather than using rewrite
, which is discouraged when avoidable.
2. www.domain -> domain
Now to redirect www to non-www, I would use a map
, avoiding to use if
:
map $host $new_host {
"~*^www\.(?<domain>.+)$" $domain;
default $host;
}
then use $new_host
in combination with return 301
3. ?dct=portfolio_ -> ?dct=services_
Finally, to redirect old domains to new ones, it is the kind of arguments filtering explained on the link provided by @wurtel, although I would once again use a map instead of if
.
map $arg_dct $new_arg_dct {
"~*^portfolio_(?<dct>.+)$" services_$dct;
default $arg_dct;
}
4. All-in-one example
http {
map $host $new_host {
"~*^www\.(?<domain>.+)$" $domain;
default $host;
}
map $arg_dct $new_arg_dct {
"~*^portfolio_(?<dct>.+)$" services_$dct;
default $arg_dct;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
return 301 https://$new_host/?dct=$new_arg_dct;
}
server {
listen 443 default_server ssl;
listen [::]:443 default_server ssl;
if ($new_host != $host) {
# Used $scheme to show you its existence
return 301 $scheme://$new_host/?dct=$new_arg_dct;
}
if ($new_arg_dct != $arg_dct) {
# Used $scheme to show you its existence
return 301 $scheme://$new_host/?dct=$new_arg_dct;
}
}
}
As you noticed, the use of seperate servers is not really necessary here, as a simple return 301 https://[...]
redirect would have been sufficient. However, I find it prettier if HTTP requests never made it to the right server. It would also help scale your configuration if you end up with services not (yet) supporting HTTPS.
As for the arguments, I took your provided examples to the letter. If you wish to have some other arguments you do not want to touch and you want to redirect in a generic fashion, you may need to chain several map
(or use if
if you know what you are doing) to rewrite the $args
variable, ending up with a rewritten construction ready to be sent to the redirection URL.
According to the documentation you can specify in requests per second or requests per minute, not requests per hour, so no it's not possible.
The rate is specified in requests per second (r/s). If a rate of
less than one request per second is desired, it is specified in
request per minute (r/m). For example, half-request per second
is 30r/m.
I suggest you round up or round down, according to your use case. 1r/s is 60 requests per minute, 2r/s is 120 requests per minute.
Best Answer
By default,
nginx
will return a503 service temporarily unavailable
error code.The
limit_req_status
directive exists to change the error code in case they hit thelimit_req
:The problem is that this directive only allows a range from 400 to 599, so you cannot specify a
301
:So, if your main issue is to display a custom error message instead of the default
503
one, you could proceed like this :Then your custom
503.html
file :The
rate
argument is mandatory. You have to specify a rate forlimit_req_zone