如何在Nginx中将https:// www重定向到非www https版本

时间:2020-07-22 19:08:27

标签: nginx redirect https nginx-config

我需要有关Nginx配置的帮助,因此我所有的HTTP访问量都流向https,而且www更改为非www。我的服务器正在运行ubuntu 18.04和nginx。

这是我的网站可用的.conf文件

server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;
    return 301 https://example.com$request_uri;
}


server {
    listen 80;
    listen [::]:80;
    server_name cdn.example.com;
    return 301 https://cdn.example.com$request_uri;
}

server {
    listen 443 http2;
    ssl on;
    ssl_certificate /etc/letsencrypt/live/cdn.example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/cdn.example.com/privkey.pem; # managed by Certbot

    root /var/www/html/storage;
    index index.php index.html index.htm;

    server_name cdn.example.com;

    location ~* \.(jpg|jpeg|png|ico|js|css|json)$ {
        expires 365d;
    }

    location / {
    aio threads;
    limit_rate_after 1m;
    limit_rate 3m;
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }

    ssl_certificate /etc/letsencrypt/live/example.com-0001/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com-0001/privkey.pem; # managed by Certbot
}

server {
    listen 443 ssl;
    ssl on;
    ssl_certificate /etc/letsencrypt/live/example.com-0001/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com-0001/privkey.pem; # managed by Certbot

    root /var/www/html/;
    index index.php index.html index.htm;

    server_name example.com www.example.com;

    rewrite ^/index.php/(.*) /$1  permanent;

        location ~* \.(jpg|jpeg|png|ico|js|css|json)$ {
        expires 365d;
    }

    location / {
    aio threads;
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}

我尝试了许多不同的配置,但是没有一个适合我。

0 个答案:

没有答案