I have a nginx webserver with 3 virtual sites(including the main real site). Using http simple works fine, using https(without redirecting) works fine. I want to redirect all http request to https, even for containers(also known as virtual hosts). But every client(firefox, links) give 301 error cyclic redirect.

This is my configuration


user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
    worker_connections 768;
http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    server_tokens off; 
    more_clear_headers Server;
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    ssl_prefer_server_ciphers on;
    ssl_certificate /etc/ssl/certs/http2.mysite.priv.crt;
    ssl_certificate_key /etc/ssl/private/http2.mysite.priv.key;
    access_log /var/log/nginx/access.log ;
    error_log /var/log/nginx/error.log debug;
    gzip on;
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

the default site

server {
    listen 80 default_server;
    listen 443 ssl ;
    root /var/www/html;
    index index.html index.htm index.nginx-debian.html;
    server_name http2.mysite.priv;
    location / {
        try_files $uri $uri/ =404;

the first container/virtual host

server {
    listen 80; 
    listen [::]:80 ;
    listen 443 ssl;
    root /var/www/html/virtual1.mysite.priv;
    index index.html index.htm index.nginx-debian.html;
    server_name virtual1.mysite.priv;
    rewrite ^(.*) https://virtual1.mysite.priv$1 permanent;
    location / {
        try_files $uri $uri/ =404;
    location /images {
        autoindex on;

I have also tried the "return" method instead of rewrite but give me the same result.

return 301 https://$host$request_uri;

What I miss?

1 Answer 1


Create one server block listening on port 80 with your return statement. Remove the listen 80 statements from all of the other server blocks.

For example:

server {
    listen 80 default_server;
    return 301 https://$host$request_uri;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.