AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1083283
Accepted
AtomX
AtomX
Asked: 2021-11-12 10:42:35 +0800 CST2021-11-12 10:42:35 +0800 CST 2021-11-12 10:42:35 +0800 CST

如何解决非 www 重定向到 www Nginx?

  • 772

这是我的 NGinx Web 服务器配置

    server {
        if ($host ~ ^[^.]+\.betafox\.net$) {
            return 301 https://$host$request_uri;
        } # managed by Certbot
    
    
        if ($host = www.betafox.net) {
            return 301 https://$host$request_uri;
        } # managed by Certbot
    
    
        if ($host = betafox.net) {
            return 301 https://$host$request_uri; 
        } # managed by Certbot
    
        listen 80;
        listen [::]:80;
    
        #server_name _;
        root /var/www/html;
    
    server_name betafox.net *.betafox.net;
        #return 301 https://$host$request_uri;
        index index.php index.html index.htm;
        location / {
            # try_files $uri $uri/ =404;
            try_files $uri $uri/ /index.php?q=$uri&$args;
            proxy_pass  https://betafox.net/;
            proxy_redirect  https://betafox.net/ $host;
            proxy_set_header Accept-Encoding "";
            proxy_ssl_server_name on;
        }
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            #fastcgi_pass 127.0.0.1:9000;
            #fastcgi_pass unix:/run/php/php8.0-fpm.sock;
            fastcgi_pass unix:/etc/alternatives/php-fpm.sock;
     }
    
    
    }
    
    server {
    
    listen 443 ssl default_server;
            listen [::]:443 ssl default_server;
    
            root /var/www/html;
            index index.php index.html index.htm;
    
          # server_name _;
            server_name betafox.net *.betafox.net;
            # Maximum file upload size is 4MB - change accordingly if needed
            client_max_body_size 512M;
            client_body_buffer_size 128k;
            include snippets/letsencrypt-nginx-certs.conf;
            include snippets/letsencrypt-nginx-route.conf;
    
            location / {
                    # try_files $uri $uri/ =404;
                    try_files $uri $uri/ /index.php?q=$uri&$args;
            }
    error_page 404 /404.html;
    
            error_page 500 502 503 504 /50x.html;
            location = /50x.html {
                    root /usr/share/nginx/html;
            }
    
            location ~ \.php$ {
                    include snippets/fastcgi-php.conf;
                    #fastcgi_pass 127.0.0.1:9000;
                    #fastcgi_pass unix:/var/run/php8.0-fpm.sock;
                    fastcgi_pass unix:/etc/alternatives/php-fpm.sock;
            }
        ssl_certificate /etc/letsencrypt/live/betafox.net-0001/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/betafox.net-0001/privkey.pem; # managed by Certbot
    
    }

当我为我的 FQDN 和子域安装 SSL 证书时,Certbot 自动修改了大部分内容。我面临的问题是关于 URL 重定向。原始 URL 是www.betafox.net,当用户键入 betafox.net 时,将重定向到https://betafoxnet.www.betafox.net/并且有消息说:您要查找的站点不存在。

我只希望所有键入 betafox.net 的用户都被转发到www.betafox.net。我相信 Nginx 可以做到这一点。我怎样才能做到这一点?

linux redirect nginx
  • 1 1 个回答
  • 56 Views

1 个回答

  • Voted
  1. Best Answer
    Tero Kilkanen
    2021-11-15T06:59:59+08:002021-11-15T06:59:59+08:00

    不幸的是,Certbot 使用if有问题的$host变量创建了 nginx 重定向。

    最好将重定向放在单独的server部分中,如下所示。

    # Redirect all requests to betafox.net URLs to corresponding www.betafox.net URLs
    server {
        listen 80;
        listen 443 ssl http2;
    
        ssl_certificate /etc/letsencrypt/live/betafox.net-0001/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/betafox.net-0001/privkey.pem; # managed by Certbot
    
        server_name betafox.net;
    
        return 301 https://www.betafox.net$request_uri;
    }
    
    # Redirect all other subdomain HTTP requests to HTTPS.
    server {
        listen 80;
    
        server_name *.betafox.net;
    
        return 301 https://$http_host$request_uri;:
    }
    
    # Removed the server block for port 80, it looked meaningless
    
    server {
        # Removed the default_server, default_server should not be the actual website
        listen 443 ssl;
        listen [::]:443 ssl;
    
        root /var/www/html;
        index index.php index.html index.htm;
    
        server_name betafox.net *.betafox.net;
        # Maximum file upload size is 4MB - change accordingly if needed
        client_max_body_size 512M;
        client_body_buffer_size 128k;
        include snippets/letsencrypt-nginx-certs.conf;
        include snippets/letsencrypt-nginx-route.conf;
    
        location / {
            try_files $uri $uri/ /index.php?q=$uri&$args;
        }
        error_page 404 /404.html;
    
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root /usr/share/nginx/html;
        }
    
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/etc/alternatives/php-fpm.sock;
        }
        ssl_certificate /etc/letsencrypt/live/betafox.net-0001/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/betafox.net-0001/privkey.pem; # managed by Certbot
    }
    
    • 1

相关问题

  • 多操作系统环境的首选电子邮件客户端

  • 你最喜欢的 Linux 发行版是什么?[关闭]

  • 更改 PHP 的默认配置设置?

  • 保护新的 Ubuntu 服务器 [关闭]

  • (软)Ubuntu 7.10 上的 RAID 6,我应该迁移到 8.10 吗?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve