V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
nonozone
V2EX  ›  NGINX

请教一个 Ngxin 换域名 301 重定向的问题,两个域名都带 ssl

  •  
  •   nonozone ·
    nonozone · 2017-11-29 14:53:34 +08:00 · 2827 次点击
    这是一个创建于 2584 天前的主题,其中的信息可能已经有所发展或是发生改变。
    之前做了一个网站,采用的是个人备案,因为有博客性质和留言功能,涉及到交互,为了避免以后出现隐患,打算还是换个域名并且采用备案。

    目前两个域名都已经备案,并且可以实现两个域名访问都可以小绿锁了。

    现在的问题是,如果直接访问 b.com ,是可以跳转到 a.com 的,但是如果访问 b.com/123.html 就不会跳转到 a.com/123.html,b.com/123.html 可以正常访问。

    这个是怎么回事?
    9 条回复    2017-11-29 20:58:51 +08:00
    LokiSharp
        1
    LokiSharp  
       2017-11-29 15:17:40 +08:00
    贴设置
    nonozone
        2
    nonozone  
    OP
       2017-11-29 15:24:12 +08:00
    因为涉及到两个域名以及两个 ssl 证书,采用了比较笨的方法。两个配置文件。
    a.com.conf

    server {
    server_name www.a.com a.com;

    #301 配置
    if ($host != 'www.b.com') {
    rewrite ^/(.*)$ https://www.b.com/$1 permanent;
    }

    #其他的配置参数

    }

    server {
    listen 80;
    listen 443 ssl;

    # SSL 证书配置
    ssl_certificate /etc/letsencrypt/live/www.a.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/www.a.com/privkey.pem;

    #ssl_certificate /etc/letsencrypt/live/www.b.com/fullchain.pem;
    #ssl_certificate_key /etc/letsencrypt/live/www.b.com/privkey.pem;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;

    server_name www.a.com;

    root /srv/www/a.com/public_html;
    access_log /var/log/www/a.com.access.log;
    error_log /var/log/www/a.com.error.log;

    location / {
    index index.php index.html index.htm;
    if (-f $request_filename) {
    expires 30d;
    break;
    }
    try_files $uri $uri/ /index.php?$args;
    }
    # Add trailing slash to */wp-admin requests.
    rewrite /wp-admin$ $scheme://$host$uri/ permanent;

    location /nginx-status {
    stub_status on;
    access_log off;
    }
    location ~ \.php$ {
    fastcgi_pass unix:/var/run/php/php7.0-fpm.a.com.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /srv/www/a.com/public_html$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_script_name;
    include fastcgi_params;
    }
    }

    -----------
    b.com.conf


    server {
    listen 80;
    server_name www.b.com b.com www.a.com a.com;
    return 301 https://www.b.com$request_uri;

    }


    server {
    listen 80;
    listen 443 ssl;

    # SSL 证书配置
    #ssl_certificate /etc/letsencrypt/live/www.a.com/fullchain.pem;
    #ssl_certificate_key /etc/letsencrypt/live/www.a.com/privkey.pem;

    ssl_certificate /etc/letsencrypt/live/www.b.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/www.b.com/privkey.pem;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;

    server_name www.b.com;

    root /srv/www/a.com/public_html;
    access_log /var/log/www/a.com.access.log;
    error_log /var/log/www/a.com.error.log;

    location / {
    index index.php index.html index.htm;
    if (-f $request_filename) {
    expires 30d;
    break;
    }
    try_files $uri $uri/ /index.php?$args;
    }
    # Add trailing slash to */wp-admin requests.
    rewrite /wp-admin$ $scheme://$host$uri/ permanent;

    location /nginx-status {
    stub_status on;
    access_log off;
    }
    location ~ \.php$ {
    fastcgi_pass unix:/var/run/php/php7.0-fpm.a.com.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /srv/www/a.com/public_html$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_script_name;
    include fastcgi_params;
    }
    }


    @LokiSharp #1
    xujiang
        3
    xujiang  
       2017-11-29 15:27:56 +08:00
    也请帮我看看这个问题
    不怎么会用 nginx,我想把 xxxx.com 重定向为 xxxx.com/home,在 nginx 尝试了以下配置都没法生效

    rewrite ^/ /home permanent;

    rewrite ^/ xxxx.com/home permanent;

    location / {
    rewrite ^/ /home permanent;
    }
    fhy1994
        4
    fhy1994  
       2017-11-29 15:35:52 +08:00
    我的是这样配置的

    ```
    # b.com.conf
    # 定义域名
    geo $domain {
    default "https://a.com";
    }
    server {
    listen 80;
    listen 443 ssl;
    client_max_body_size 100M;
    server_name b.com;
    return 301 $domain$request_uri; # 将 b.com 301 重定向到 a.com

    以下配置省略
    ```
    0ZXYDDu796nVCFxq
        5
    0ZXYDDu796nVCFxq  
       2017-11-29 15:52:11 +08:00   ❤️ 1
    更优雅的配置:
    ```
    server {
    listen 80;
    server_name www.a.com a.com b.com;
    return 301 https://www.b.com$request_uri;
    }

    server {
    listen 443 ssl;
    server_name www.a.com a.com;
    return 301 https://www.b.com$request_uri;
    }

    server {
    listen 443 ssl;
    server_name b.com;
    return 301 https://www.b.com$request_uri;
    }
    ```
    0ZXYDDu796nVCFxq
        6
    0ZXYDDu796nVCFxq  
       2017-11-29 15:53:28 +08:00   ❤️ 1
    @xujiang
    ```
    server = / {
    return 301 /home;
    }
    ```
    nonozone
        7
    nonozone  
    OP
       2017-11-29 16:25:37 +08:00
    @gstqc #5 两个 server 中间需要添加 location 那些么?
    0ZXYDDu796nVCFxq
        8
    0ZXYDDu796nVCFxq  
       2017-11-29 16:54:00 +08:00 via iPhone
    @nonozone 不用
    msg7086
        9
    msg7086  
       2017-11-29 20:58:51 +08:00
    location = / { return 301 /home; }

    这里用「 location /」 做跳转是不行的,要用「 location = /」。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5628 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 03:24 · PVG 11:24 · LAX 19:24 · JFK 22:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.