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

求 Nginx 高手帮忙写段配置

  •  
  •   aries910 · 2021-01-08 15:25:03 +08:00 · 2388 次点击
    这是一个创建于 1176 天前的主题,其中的信息可能已经有所发展或是发生改变。

    本人有个 WP 的小网站 a.com 现在想用二级域名代替( 2.a.com )原来的域名, 但是又怕通过原来的链接地址访问不到,希望访问 a.com/aaa/bbb 能 301 跳转到 2.a.com/aaa/bbb 空闲下来的 a.com 准备指向一个静态的 html

    求高手帮帮忙指导该怎么写,主要不知道配置项怎么判断域名后面是否有路径

    8 条回复    2021-01-12 12:03:11 +08:00
    Daylight1993
        1
    Daylight1993  
       2021-01-08 15:37:14 +08:00
    location /aaa/bbb/ {
    proxy_pass http:// 2.a.com/aaa/bbb/;
    }

    这样可以不,试试看
    aries910
        2
    aries910  
    OP
       2021-01-08 15:42:27 +08:00
    @Daylight1993 不仅仅是 aaa/bbb 一条跳转啊,好多呢,总不能有一条就写一个规则啊,有没有通配的那种。。?
    lanternxx
        3
    lanternxx  
       2021-01-08 15:44:09 +08:00
    location =/ {
    root /var/www/html;
    index index.html;
    }
    return 301 https://2.example.com$request_uri;
    ruyu
        4
    ruyu  
       2021-01-08 15:48:37 +08:00
    location ~ /.+ {
    return 301 http://2.a.com$request_uri;
    }
    Daylight1993
        5
    Daylight1993  
       2021-01-08 15:48:45 +08:00
    理解错了。以为要跳转 aa/bb/底下的资源呢。那应该是楼上那样的用$request_uri; 跳转
    aries910
        6
    aries910  
    OP
       2021-01-08 15:53:04 +08:00
    @lanternxx 感谢!
    dorothyREN
        7
    dorothyREN  
       2021-01-08 17:22:28 +08:00
    配 nginx 多麻烦,这不就是一个 cname 的事嘛
    aries910
        8
    aries910  
    OP
       2021-01-12 12:03:11 +08:00
    @lanternxx
    大佬,按照你的写法,除非去除下面的 /匹配,现在总是直接跳转 blog.a.com ,感谢=/没有处于第一优先级匹配,请问下是什么原因啊
    server {
    server_name www.a.com a.com;
    listen 80 ;
    access_log /var/log/nginx/access.log;
    location /.well-known/acme-challenge/ {
    auth_basic off;
    allow all;
    root /usr/share/nginx/html;
    try_files $uri =404;
    break;
    }

    rewrite ^(.*) https://$server_name$1 permanent;
    }

    server {
    server_name www.a.com a.com;
    listen 443 ssl http2 ;
    access_log /var/log/nginx/access.log;
    ssl_session_timeout 5m;
    ssl_session_cache shared:SSL:50m;
    ssl_session_tickets off;
    ssl_certificate /etc/nginx/certs/www.a.com.crt;
    ssl_certificate_key /etc/nginx/certs/www.a.com.key;
    add_header Strict-Transport-Security "max-age=31536000" always;

    root /home;

    location = / {
    try_files '' /index.html;
    }

    location / {
    return 301 https://blog.a.com$request_uri;
    }
    }
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2606 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 15:37 · PVG 23:37 · LAX 08:37 · JFK 11:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.