用IP地址访问apache、nginx时跳转到指定域名

一、apache

1、ip访问时跳转到指定域名

修改apache站点配置文件
vim ip.conf

                                                                                                                      
    ServerName  192.168.13.131
    ServerAlias 192.168.13.131
    RewriteEngine On
    RewriteRule ^/(.*) http://www.testshop.com/$1 [R=301,NC]

2、http访问跳转https

vim www.mytest.com.conf

 
    ServerName www.mytest.com 
    ServerAlias mytest.com 
    RewriteEngine On 
    RewriteRule ^/(.*)$ https://www.mytest.com/$1 [R=301] 

二、nginx

1、ip访问时跳转到指定域名

vim vhosts.conf

server {                                    
        listen 80 default;   #配置此段表示用IP或其他域名访问时跳转到www.mytest.com
        server_name _;
        rewrite ^ http://www.mytest.com$request_uri?;
}

2、https配置、http访问跳转https
Certbot-auto 免费 SSL 证书,查看该博客。

nginx配置如下:
vim www.test.com.conf

server
{
    listen 80;
	listen 443 ssl http2;
    server_name www.test.com test.com;
    index index.php index.html index.htm default.php;
    root /www/wwwroot/www.test.com;
    
    #SSL相关配置
    if ($server_port !~ 443){
        rewrite ^(/.*)$ https://$host$1 permanent;
    }
    
    #HTTP_TO_HTTPS_END
    #  # letsencrypt生成的文件
    ssl_certificate    /etc/letsencrypt/live/www.bu85m.cn/fullchain.pem;
    ssl_certificate_key    /etc/letsencrypt/live/www.bu85m.cn/privkey.pem;
    
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    error_page 497  https://$host$request_uri;
    
    #SSL-END
    
    #ERROR-PAGE-START  错误页配置,可以注释、删除或修改
    error_page 404 /404.html;
    error_page 502 /502.html;
    #ERROR-PAGE-END
    
    #PHP配置
    location ~ \.php(.*)$ {
       fastcgi_pass   127.0.0.1:9000;
       fastcgi_index  index.php;
       fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
       fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
       fastcgi_param  PATH_INFO  $fastcgi_path_info;
       fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
       include        fastcgi_params;
    }
    #PHP-INFO-END
    
    #url重写
    location / {
	    if (!-e $request_filename) {
   		rewrite  ^(.*)$  /index.php?s=/$1  last;
    }
    
    #禁止访问的文件或目录
    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
    {
        return 404;
    }
    
    #一键申请SSL证书验证目录相关设置
    location ~ \.well-known{
        allow all;
    }
    
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
        error_log off;
        access_log off;
    }
    
    location ~ .*\.(js|css)?$
    {
        expires      12h;
        error_log off;
        access_log off; 
    }
    access_log  /www/wwwlogs/www.test.com.log;
    error_log  /www/wwwlogs/www.test.com.error.log;
}

你可能感兴趣的:(用IP地址访问apache、nginx时跳转到指定域名)