Correct way in new versions of nginx
Turn out my first answer to this question was correct at certain time, but it turned into another pitfall – to stay up to date please check Taxing rewrite pitfalls
I have been corrected by many SE users, so the credit goes to them, but more importantly, here is the correct code:
server {
listen 80;
server_name my.domain.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name my.domain.com;
# add Strict-Transport-Security to prevent man in the middle attacks
add_header Strict-Transport-Security "max-age=31536000" always;
[....]
}
Related Posts:
- WordPress redirect loop on nginx + apache reverse proxy
- Infinite loop behind SSL proxy on non-standard port
- Nginx + Redirection Plugin breaks Permalinks [closed]
- How to force or redirect to SSL in nginx?
- nginx HTTPS serving with same config as HTTP
- Nginx Redirect via Proxy, Rewrite and Preserve URL
- Remove “www” and redirect to “https” with nginx
- How can I use environment variables in Nginx.conf
- How to redirect all HTTP requests to HTTPS
- “413 Request Entity Too Large” in Nginx with “client_max_body_size” set
- Nginx: Failed to start A high performance web server and a reverse proxy server
- NGINX: upstream timed out (110: Connection timed out) while reading response header from upstream
- How to fix nginx throws 400 bad request headers on any header testing tools?
- How do you redirect HTTPS to HTTP?
- How to use NGINX as forward proxy for any requested location?
- “configuration file /etc/nginx/nginx.conf test failed”: How do I know why this happened?
- nginx – two subdomain configuration
- GeoIP.dat.gz and GeoLiteCity.dat.gz not longer available? Getting 404 trying to load it
- WordPress redirecting connections on port 8080 to 80
- Why is WordPress redirecting from http to https on a local environment?
- Stop WordPress and Plugins from Overwriting .htaccess
- How to configure nginx to redirect requests to the uploads directory to the production server?
- Force HTTPS using .htaccess – stuck in redirect loop
- Keeps on redirecting on my development copy
- wp-json/ return 404, but wp-json/wp/v2/ works fine on my nginx server
- WordPress behind Proxy – Mixed Content
- How to setup 301 redirects for multiple query string URLs?
- How to redirect url requests to https? [closed]
- Redirect www to non-www htaccess
- 302 redirect instead of 301 after switch to HTTPS
- How are both HTTP and HTTPS versions displaying?
- Redirect from https to http or from http to https? [closed]
- Redirect to https not working with .htaccess [closed]
- ERR_TOO_MANY_REDIRECTS status 302, how to configure NGINX locations to work with AWS ALB?
- Mixed Content with SSL, wordpress behind a reverse proxy
- 302 redirect to signup with nginx reverse proxy
- NGINX conf on WP Multisite enabled — subdirectory — images fail to load after updates
- Configuring Routing Rules for WordPress+Nginx and WP-SuperCache?
- Menu links only using http after enabling https, unable to redirect http links
- Cant Access Website – Changed HTTP to HTTPs
- https multiple redirects
- WordPress multisite subdirectory on nginx behind reverse proxy
- some url does not redirect from http to https
- https redirect (with .htaccess) redirect loop
- Redirect HTTP to HTTPS
- Installation failed: Could not create directory – CentOS 7
- REST API parameters not working with nginx
- How to set up htaccess to redirect a particular post type or subdirectory to HTTPS?
- Site redirecting from http to https [closed]
- WordPress Redirecting HTTPS Requests to HTTP
- Difference between these two nginx try_files statements for WordPress?
- How to test drive WordPress on VPS with LEMP stack before going live?
- https redirect hell (adding www)
- Site kit Google Analytics setup giving Error: {“error”:”invalid ‘redirect_uri'”}
- nginx.conf appeared in all WordPress installations on folder
- Moving from http to https, and www. to non-www URL
- nginx php-fpm, and auto updates?
- What is the right way to redirect all traffic to HTTPS?
- WordPress and nginx, download some pages instead of serving them
- nginx wordpress subdirectory Invalid post type
- wp_enqueue_style – CSS Not loading – ERR_TOO_MANY_REDIRECTS [closed]
- ERROR: No such service: nginx when building wordpress with docker [closed]
- Nginx tries the wrong file when http_cookie matches logged_in
- Htaccess file isn’t redirecting http sub-pages to https––they display 404 error instead, tried many solutions and none work
- Site resolves to www in Firefox and without in Chrome
- Redirect HTTP to HTTPS for all sub domains (blog posts)
- 301 redirection editing .htaccess doesn’t work
- I get redirected too many times only in wp-admin?
- WordPress keeps redirecting subpages without http to https homepage after switch
- WordPress Nginx Won’t 404
- WordPress file not found and 403 forbidden
- https – too many redirects
- Nginx config, how can I redirect primary multisite domain, but not its sub-folders, nor other domains?
- Receiving 404 when uploading file larger than 10kb
- How to use https on one page only?
- Too many redirects after added SSL redirect on port 80 virtual host
- Correct regex for Nginx FastCGI cache?
- .htaccess 301 redirect conflict with plugin “WordPress Https”
- ReDirect subfolder link to another sub-folder and force SSL
- Htaccess https 301 problem
- Random occurences of 502 Bad Gateway using WordPress and Nginx
- SSL via different domain & path with NGINX/PHP-FPM/WP
- Limit file downloads to logged in users (WP + Nginx)
- Blog has an infinite redirect loop
- /blog/wp-json/ nginx return 404
- Force WordPress https redirect before www redirect based on site address url
- WordPress with Photocrati Theme keeps resetting site URL – help solving underlying problem
- How to make my site use HTTPS for images or how to insert images as relative paths?
- Moving wordpress to different url/server creates redirect loop
- WordPress CSS/Theme gone after SSL got activated
- My site doesn’t redirect from HTTP to HTTPS
- NGINX / WordPress Site – Increasing php.ini max_execution_time = 600 doesn’t work
- Only execute function if SSL is set up properly
- Admin sometimes redirects to HTTPS
- Nginx redirects to an extra subdirectory
- HTTPS to HTTP rewrite rules not working as expected
- Something is injecting a slash after any static file response , server is nginx
- Custom permalinks not work on Nginx + Apache
- Last-Modified header is removed from server response!
- Do I need a DNS on a home network?