nginx配置两个域名url之间跳转(https配置)

Cherish
2021-03-10 / 0 评论 / 179 阅读
温馨提示:
本文最后更新于2021-03-10,若内容或图片失效,请留言反馈。

1.环境

  • nginx/1.18.0
  • CentOS6.8

2.目的

有两个域名,分别是owye.cccherishspring.cn,现在我想保留的域名是cherishspring.cn这个域名,但是以前owye.cc的流量又不想浪费,所以我想实现以前所有http://www.owye.cc/的链接,全部跳转到http://www.cherishspring.cn

3.配置

域名cherishspring.cn的配置不变,将owye.cc的配置更改为如下内容:

server{
    listen 80;
    ## listen 443 ssl;
    server_name www.owye.cc owye.cc;
    ## 将所有http请求跳转到新地址
    return 301 $scheme://www.cherishspring.cn$request_uri;
}

这里用到了nginx的两个内置变量:

$scheme表示请求模式,即httpshttp;

$request_uri表示完整的原始请求URI(包括参数)。

配置完成,可以通过访问owye.cccherishspring.cn体验一下。

我的配置如下:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  www.owye.cc ;
        server_name owye.cc;
        return 301 $scheme://www.cherishspring.cn$request_uri;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;

            proxy_pass http://127.0.0.1;

        }

        #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   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}