1

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() } } } } 

1 Answer 1

-1

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() } } } }

Sign up to request clarification or add additional context in comments.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.