使用 NGINX,如何在使用子域名时将 www 重定向到非 www?

使用 NGINX,如何在使用子域名时将 www 重定向到非 www?

我使用 NGINX 和 PHP-FPM 通过 wordpress 建立了一个网站。

backend.site.com用于 wordpress-backend

site.com用于 nuxt-frontend

我的问题

当我通过输入访问前端时,www.site.com我被重定向到backend.site.com

# /etc/nginx/conf.d/default.conf

server {
    listen 80;

    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    ssl_certificate     /etc/nginx/ssl/site.com_ssl_certificate.cer;
    ssl_certificate_key /etc/nginx/ssl/site.com_private_key.key;

    return 301 https://$host$request_uri;
}
# /etc/nginx/conf.d/site.com.conf

map $sent_http_content_type $expires {
    "text/html"                 epoch;
    "text/html; charset=utf-8"  epoch;
    default                     off;
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;

    ssl_certificate     /etc/nginx/ssl/site.com_ssl_certificate.cer;
    ssl_certificate_key /etc/nginx/ssl/site.com_private_key.key;

    server_name site.com www.site.com;

    gzip            on;
    gzip_types      text/plain application/xml text/css application/javascript;
    gzip_min_length 1000;

    location ^~ / {
        alias /var/www/html/web-frontend/.nuxt/dist/client;

        expires $expires;
        proxy_set_header Host               $host;
        proxy_set_header X-Real-IP          $remote_addr;
        proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto  $scheme;
        proxy_read_timeout          1m;
        proxy_connect_timeout       1m;
        proxy_pass                          http://127.0.0.1:3000;
    }

    location ~ /\.ht {
                deny all;
    }

    location = /favicon.ico {
            log_not_found off; access_log off;
    }

    location = /robots.txt {
            log_not_found off; access_log off; allow all;
    }

    location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
            expires max;
            log_not_found off;
    }
}
# /etc/nginx/conf.d/backend.site.com.conf

server {
    # SSL configuration
    #
    listen 443 ssl;
    listen [::]:443 ssl;
    ssl_certificate     /etc/nginx/ssl/site.com_ssl_certificate.cer;
    ssl_certificate_key /etc/nginx/ssl/site.com_private_key.key;

    root /var/www/html/web-backend/web;

    # Add index.php to the list if you are using PHP
    index index.php;

    server_name backend.site.com;

    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_min_length 0;
    gzip_types text/plain application/javascript text/css text/xml application/xml application/xml+rss text/javascript application/vnd.ms-fontobject application/x-font-ttf font/opentype;

    client_max_body_size 100M;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/run/php/php7.4-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME   $document_root$fastcgi_script_name;
            fastcgi_param SCRIPT_NAME   $fastcgi_script_name;
        }

    location ~ /\.ht {
            deny all;
    }

    location = /favicon.ico {
            log_not_found off; access_log off;
    }
    location = /robots.txt {
            log_not_found off; access_log off; allow all;
    }
    location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
            expires max;
            log_not_found off;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

答案1

您对虚拟主机的工作原理存在一些误解。请检查以下几点:

  1. 配置虚拟主机时,您需要为 http(端口 80)和 https(端口 443)设置虚拟主机。
  2. 将端口 80 上的虚拟主机重定向到端口 443 上的虚拟主机。
  3. 请记住,始终需要处理 http(端口 80)的传入流量。为什么?有些开发人员(我不知道为什么)使用相同的虚拟主机发布两个不同的应用程序,一个在端口 80 上,另一个在 443 上。

尝试添加这个来解决你的问题

此配置将 80 端口的流量重定向到 https。

server {
    listen 80; 
    server_name  site.com www.site.com;
    access_log  /var/log/nginx/site.com.access.log  main;
    error_log /var/log/nginx/site.com.error.log  error;
  
    location / {
        return 301 https://$host$request_uri;
    }
}

将端口传入的流量重定向backend.site.com:80backend.site.com:443虚拟主机。

笔记:我添加了:80突出显示连接通过哪个端口传入

server {
    listen 80;
    server_name  backend.site.com;
    access_log  /var/log/nginx/backend.site.com.access.log  main;
    error_log /var/log/nginx/backend.site.com.error.log  error;
   
    location / {
        return 301 https://$host$request_uri;
    }
}

相关内容