Sure. This is where the minimal nginx configuration is done:
server { listen 80 default_server; server_name _; location / { rewrite ^.*$ http://squishling.co.uk/errors/incorrect_subdomain.html redirect; } } server { listen 80; server_name squishling.co.uk; location / { echo some page; } location /errors/incorrect_subdomain.html { echo incorrect subdomain error; } }
Here's how to run it:
mkdir conf.d- put the configuration file above in
conf.d/main.conf docker run -p80:80 -v ~/work/test/subreq/conf.d:/etc/nginx/conf.d openresty/openresty:alpine
testing
squishling.co.uk and foo.squishling.co.uk are overridden in my /etc/hosts , so they are routed to my localhost
curl -D - http://squishling.co.uk HTTP/1.1 200 OK Server: openresty/1.13.6.1 Date: Tue, 06 Feb 2018 18:49:25 GMT Content-Type: application/octet-stream Transfer-Encoding: chunked Connection: keep-alive some page
Now try the wrong domain
curl -D - http://foo.squishling.co.uk HTTP/1.1 302 Moved Temporarily Server: openresty/1.13.6.1 Date: Tue, 06 Feb 2018 18:49:34 GMT Content-Type: text/html Content-Length: 167 Connection: keep-alive Location: http://squishling.co.uk/errors/incorrect_subdomain.html <html> <head><title>302 Found</title></head> <body bgcolor="white"> <center><h1>302 Found</h1></center> <hr><center>openresty/1.13.6.1</center> </body> </html>
Or the same after redirecting:
curl -L -D - http://foo.squishling.co.uk HTTP/1.1 302 Moved Temporarily Server: openresty/1.13.6.1 Date: Tue, 06 Feb 2018 18:50:03 GMT Content-Type: text/html Content-Length: 167 Connection: keep-alive Location: http://squishling.co.uk/errors/incorrect_subdomain.html HTTP/1.1 200 OK Server: openresty/1.13.6.1 Date: Tue, 06 Feb 2018 18:50:03 GMT Content-Type: text/html Transfer-Encoding: chunked Connection: keep-alive incorrect subdomain error
So, the main idea is that you define a default server configuration that will intercept all domains except those that have individual configurations. The same can be done on other web servers.
source share