skip to Main Content

Im trying to setup openresty in ubuntu. every things is working fine, even ssl is also working fine but i didnot find way to redirect www to non www. here is my configurations.

http {
    lua_shared_dict auto_ssl 1m;
    lua_shared_dict auto_ssl_settings 64k;
    resolver 8.8.8.8 ipv6=off;
  
    map $host $no_www_host {
        default $host;
        "~^www.(.*)$" $1;
    }
    init_by_lua_block {
        auto_ssl = (require "resty.auto-ssl").new()
        auto_ssl:set("allow_domain", function(domain)
        return true
        end)
        auto_ssl:init()
    }

    init_worker_by_lua_block {
        auto_ssl:init_worker()
    }
    server {
        listen 443 ssl;
        ssl_certificate_by_lua_block {
            auto_ssl:ssl_certificate()
        }
        ssl_certificate /etc/ssl/resty-auto-ssl-fallback.crt;
        ssl_certificate_key /etc/ssl/resty-auto-ssl-fallback.key;
    location / {
        include proxy_params;
        proxy_pass http://unix:/run/ecommerce_demo.sock;
        }
    }

    server {
        listen 80;
        location /.well-known/acme-challenge/ {
            content_by_lua_block {
                auto_ssl:challenge_server()
            }
        }

        location / {
            return 301 https://$no_www_host$request_uri; # Redirect all HTTP to HTTPS
        }
    }

    server {
        listen 127.0.0.1:8999;
        client_body_buffer_size 128k;
        client_max_body_size 128k;

        location / {
            content_by_lua_block {
                auto_ssl:hook_server()
            }
        }
    }
}

2

Answers


  1. To redirect www to non-www in OpenResty, you can achieve this by adding an additional server block in your http configuration. The server block for http://www.example.com should redirect requests to example.com using a 301 redirect, while keeping SSL intact.

    http {
        lua_shared_dict auto_ssl 1m;
        lua_shared_dict auto_ssl_settings 64k;
        resolver 8.8.8.8 ipv6=off;
    
        # Map to remove 'www.' from hostnames
        map $host $no_www_host {
            default $host;
            "~^www.(.*)$" $1; # Matches 'www.' and removes it
        }
    
        # Auto SSL initialization
        init_by_lua_block {
            auto_ssl = (require "resty.auto-ssl").new()
            auto_ssl:set("allow_domain", function(domain)
                return true
            end)
            auto_ssl:init()
        }
    
        init_worker_by_lua_block {
            auto_ssl:init_worker()
        }
    
        # Redirect www to non-www (SSL 443)
        server {
            listen 443 ssl;
            ssl_certificate_by_lua_block {
                auto_ssl:ssl_certificate()
            }
            ssl_certificate /etc/ssl/resty-auto-ssl-fallback.crt;
            ssl_certificate_key /etc/ssl/resty-auto-ssl-fallback.key;
    
            # Server block specifically for 'www' redirection
            server_name www.*;
    
            # Redirect 'www' to non-'www'
            return 301 https://$no_www_host$request_uri;
        }
    
        # Standard SSL server
        server {
            listen 443 ssl;
            ssl_certificate_by_lua_block {
                auto_ssl:ssl_certificate()
            }
            ssl_certificate /etc/ssl/resty-auto-ssl-fallback.crt;
            ssl_certificate_key /etc/ssl/resty-auto-ssl-fallback.key;
    
            # No redirection here, serves your backend or application
            location / {
                include proxy_params;
                proxy_pass http://unix:/run/ecommerce_demo.sock;
            }
        }
    
        # HTTP to HTTPS & www-to-non-www redirection
        server {
            listen 80;
    
            # ACME challenge for Let's Encrypt
            location /.well-known/acme-challenge/ {
                content_by_lua_block {
                    auto_ssl:challenge_server()
                }
            }
    
            # Redirect all HTTP requests (www and non-www) to HTTPS non-www
            location / {
                return 301 https://$no_www_host$request_uri;
            }
        }
    
        # Auto SSL hook server (internal use only)
        server {
            listen 127.0.0.1:8999;
            client_body_buffer_size 128k;
            client_max_body_size 128k;
    
            location / {
                content_by_lua_block {
                    auto_ssl:hook_server()
                }
            }
        }
    }
    
    Login or Signup to reply.
  2. You can use this configurations for HTTP to HTTPS redirect.

    http {
        lua_shared_dict auto_ssl 1m;
        lua_shared_dict auto_ssl_settings 64k;
        resolver 8.8.8.8 ipv6=off;
      
        map $host $no_www_host {
            default $host;
            "~^www.(.*)$" $1;
        }
    
        init_by_lua_block {
            auto_ssl = (require "resty.auto-ssl").new()
            auto_ssl:set("allow_domain", function(domain)
                return true
            end)
            auto_ssl:init()
        }
    
        init_worker_by_lua_block {
            auto_ssl:init_worker()
        }
    
        # SSL-enabled server
        server {
            listen 443 ssl;
            ssl_certificate_by_lua_block {
                auto_ssl:ssl_certificate()
            }
            ssl_certificate /etc/ssl/resty-auto-ssl-fallback.crt;
            ssl_certificate_key /etc/ssl/resty-auto-ssl-fallback.key;
    
            # Redirect www to non-www for HTTPS
            if ($host ~* ^www.(.*)$) {
                return 301 https://$1$request_uri;
            }
    
            location / {
                include proxy_params;
                proxy_pass http://unix:/run/ecommerce_demo.sock;
            }
        }
    
        # HTTP to HTTPS redirection
        server {
            listen 80;
            location /.well-known/acme-challenge/ {
                content_by_lua_block {
                    auto_ssl:challenge_server()
                }
            }
    
            location / {
                return 301 https://$no_www_host$request_uri;
            }
        }
    
        # Separate server block to handle www redirection for HTTP
        server {
            listen 80;
            server_name ~^www.(.*)$;
    
            location / {
                return 301 http://$1$request_uri;
            }
        }
    
        # Auto SSL Hook Server
        server {
            listen 127.0.0.1:8999;
            client_body_buffer_size 128k;
            client_max_body_size 128k;
    
            location / {
                content_by_lua_block {
                    auto_ssl:hook_server()
                }
            }
        }
    }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search