0%

Nginx配置IP访问跳转域名

nginx配置访问ip跳转域名,http跳转https。

1
2
3
>nginx -V
nginx version: nginx/1.14.2
built with OpenSSL 1.1.1d 10 Sep 2019
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
http
{
........sometext........
server{
listen 80;
#return 500;
rewrite ^(.*) https://Your_Domain_Name permanent;
}
server{
listen 443 ssl;
root /var/www/html/; #web根路径
ssl_certificate /tmp/nginx.crt; #ssl证书
ssl_certificate_key /tmp/nginx.key;
keepalive_timeout 70;
server_name Your_Domain_Name;
if ($host != 'Your_Domain_Name')
{
#return 500; #返回指定http响应码
rewrite ^(.*) https://Your_Domain_Name permanent;
}
}
........sometext........
}

如果出现

1
nginx: [emerg] unknown directive "if($host" in /usr/local/nginx/nginx.conf:60

则可能nginx版本较低对配置文件格式要求比较严格。if和( 要有空格,$host前后要有空格。

更详细配置可看: