Nginx Configuration HTTPS &&SSL CERT and HTTP rewrite HTTPS

为了更好的针对相应网站做HTTPS,就不对大的nginx.conf做配置了,对小的nginx.conf配置就好
1、下载nginx
2、进到/etc/nginx/conf.d/创建xxx.conf
3、对xxx.conf进行配置
一、下载Nginx
在/etc/yum.repos.d/创建个nginx.repo源

1
2
# touch /etc/yum.repos.d/nginx.repo
# vi /etc/yum.repos.d/nginx.repo

然后把这一段写到nginx.repo源里面

1
2
3
4
5
6
7
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/OS/OSRELEASE/$basearch/
gpgcheck=0
enabled=1
接着保存
:wq!

1
2
3
4
5
baseurl=http://nginx.org/packages/mainline/OS/OSRELEASE/$basearch/中的/OS/OSRELEASE需替换成当前的系统和版本,例如Centos7,那么OS需要改成centos,OSRELEASE需要改成7即可

接着就可以直接安装Nginx了

# yum -y install nginx

还有另一种方法就是安装epel源来达到安装nginx的目的

1
2
sudo yum -y install epel-release
# sudo yum install nginx

二、对/etc/nginx/conf.d/底下的xxx.conf进行配置
为了方便、更好的区分哪个网站是哪个配置文件,就在conf.d/文件夹里创建个conf文件
例如我建了个导航页网站,那我就进入到conf.d/文件夹中创建个Navigation_Page.conf
然后对Navigation_Page.conf配置http

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
server {
listen 80;
server_name dh.daishenghui.club;
server_name isdai.tk ;

#charset koi8-r;
#access_log /etc//nginx/logs/access.log main;

if ($host ~* "^dh.daishenghui.club$") {
rewrite ^/(.*)$ https://dh.daishenghui.club/ permanent;
}


location / {
root /www/Navigation_page;
index index.html index.htm;
}

#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 /usr/share/nginx/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;
#}
}

1

三、HTTP配置好后,就是配置HTTPS了
先创建一个ssl证书存放的位置

1
2
3
4
5
6
7
8
# cd /etc/nginx/conf.d/
# mkdir ssl_certs/
# cd ssl_certs/
# mkdir dh.daishenghui.club/
# cd dh.daishenghui.club/
然后把 dh.daishenghui.club 这个域名的SSL证书 上传到 dh.daishenghui.club/ 这个文件夹里面
建议都以域名的名字来创建存放该域名SSL证书的文件夹
例如 dh.daishenghui.club 这个域名,就创建 dh.daishenghui.club/文件夹存放这个域名的SSL证书

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
server {
listen 443 ssl;
server_name dh.daishenghui.club;
ssl on;
root html;
index index.html index.htm;
ssl_certificate /etc/nginx/conf.d/ssl_certs/dh.daishenghui.club/1_dh.daishenghui.club_bundle.crt;
ssl_certificate_key /etc/nginx/conf.d/ssl_certs/dh.daishenghui.club/2_dh.daishenghui.club.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
root /www/Navigation_page;
index index.html index.htm;
}
#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 /usr/share/nginx/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;
#}
}

2

四、HTTP和HTTPS都配置好了之后,可以做个强制跳转到HTTPS的配置
在这段配置下面加强制跳转的配置就行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
server {
listen 80;
server_name dh.daishenghui.club;
server_name isdai.tk ;

server_name isdai.tk;下面加上

if ($host ~* "^dh.daishenghui.club$") {
rewrite ^/(.*)$ https://dh.daishenghui.club/ permanent;
   }  


如果也想输入www后跳转到https的话,可以在上面语句结束的大括号后面在写一句一模一样的语句

if ($host ~* "^dh.daishenghui.club$") {
   rewrite ^/(.*)$ https://www.daishenghui.club/ permanent;
}

然后保存并退出,重启nginx即可生效

:wq!

1
2
3
# systemctl restart nginx
或者
# service nginx restart

3

在/etc/nginx/conf.d/里面创建的xxx.conf配置文件只需要写server就行了,不需要像大文件里面还有个user啥的开头

关注我的公众号吧~戴戴的Linux

文章目录


本站总访问量 本文总阅读量