You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nginx配置:将所有请求重定向至https://mysite.com.au

Fix Nginx Redirects for Your Domain

Let's get your redirects sorted so all traffic lands on https://mysite.com.au no matter how users type your URL. Here's the complete Nginx config you need, broken down into logical parts:

1. Redirect All HTTP Traffic to HTTPS

First, create a server block that listens on port 80 (regular HTTP) for both your main domain and www subdomain. This catches anyone who visits without https://:

server {
    listen 80;
    listen [::]:80;
    server_name mysite.com.au www.mysite.com.au;

    # Redirect all HTTP requests to HTTPS main domain
    return 301 https://mysite.com.au$request_uri;
}

2. Redirect HTTPS www to Non-WWW

Next, add a server block for the HTTPS www subdomain. This ensures even if someone types https://www.mysite.com.au, they get sent to the correct URL:

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name www.mysite.com.au;

    # Use your existing Let's Encrypt certificates
    ssl_certificate /etc/letsencrypt/live/mysite.com.au/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mysite.com.au/privkey.pem;

    # Redirect HTTPS www to non-www main domain
    return 301 https://mysite.com.au$request_uri;
}

3. Your Main HTTPS Server Block

Keep your original server block (updated to only handle mysite.com.au) to serve your actual website content:

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name mysite.com.au;

    root /www/mysite.com.au;

    # Let's Encrypt certificates
    ssl_certificate /etc/letsencrypt/live/mysite.com.au/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mysite.com.au/privkey.pem;

    # Optional: Add recommended SSL security settings (good practice)
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;

    # Add your other site config here (like location blocks for PHP, static files, etc.)
    location / {
        try_files $uri $uri/ =404;
    }
}

Key Notes:

  • We use 301 permanent redirects so search engines and browsers cache the redirect, which helps with SEO and consistent user experience.
  • $request_uri preserves the original path (so someone visiting http://www.mysite.com.au/about gets sent to https://mysite.com.au/about instead of just the homepage).
  • Double-check that your Let's Encrypt certificate covers both mysite.com.au and www.mysite.com.au (it should if you included both domains when issuing the certificate).

Once you've updated your config, test it for syntax errors with nginx -t, then reload Nginx with systemctl reload nginx (Debian/Ubuntu) or service nginx reload (RHEL/CentOS) to apply the changes.

内容的提问来源于stack exchange,提问作者maxflow

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:32:17