Nginx下载与安装
官网:http://nginx.org/,下载:http://nginx.org/en/download.html
Nginx的基本的操作
1start nginx // 启动Nginx, Windows命令,一闪而过
2nginx // 启动Nginx,在nginx的可执行文件目录下
3nginx -s stop // 停止nginx
4nginx -s reload // 重新加载配置文件
5nginx -s quit // 退出nginx
6nginx -t // 检测nginx的配置文件是否正确
Nginx的基本目录
Nginx的最重要的几个文件夹如下,conf,html,logs。conf存放配置文件,html是存放静态文件的,可以修改,logs存放日志。
Nginx的基本配置介绍
Nginx最基本的作用就是负载均衡和反向代理。nginx运行时需要在nginx.conf之中配置,nginx.conf是主配置文件,还可以在nginx.conf之中加载其他的配置文件,引入即可。这样方便不同的项目区分,每个项目可以使用自己对应的nginx配置文件。
1worker_processes 1;
2
3#error_log logs/error.log;
4#error_log logs/error.log notice;
5#error_log logs/error.log info;
6
7#pid logs/nginx.pid;
8
9
10events {
11 worker_connections 1024;
12}
13
14
15http {
16 include mime.types;
17 default_type application/octet-stream;
18
19 #access_log logs/access.log main;
20
21 sendfile on;
22 #tcp_nopush on;
23
24 #keepalive_timeout 0;
25 keepalive_timeout 65;
26
27 #gzip on;
28
29 server {
30 listen 80;
31 server_name localhost;
32
33 #charset koi8-r;
34
35 #access_log logs/host.access.log main;
36
37 location / {
38 root html;
39 index index.html index.htm;
40 }
41
42 #error_page 404 /404.html;
43
44 # redirect server error pages to the static page /50x.html
45 #
46 error_page 500 502 503 504 /50x.html;
47 location = /50x.html {
48 root html;
49 }
50 }
51
52
53 # another virtual host using mix of IP-, name-, and port-based configuration
54 #
55 #server {
56 # listen 8000;
57 # listen somename:8080;
58 # server_name somename alias another.alias;
59
60 # location / {
61 # root html;
62 # index index.html index.htm;
63 # }
64 #}
65
66
67 # HTTPS server
68 #
69 #server {
70 # listen 443 ssl;
71 # server_name localhost;
72
73 # ssl_certificate cert.pem;
74 # ssl_certificate_key cert.key;
75
76 # ssl_session_cache shared:SSL:1m;
77 # ssl_session_timeout 5m;
78
79 # ssl_ciphers HIGH:!aNULL:!MD5;
80 # ssl_prefer_server_ciphers on;
81
82 # location / {
83 # root html;
84 # index index.html index.htm;
85 # }
86 #}
87}
上面是经过简化的nginx.conf,是nginx之中默认提供的,上述可以简化成为下面三部分,主要就是worker_processes
,events
和http
,worker_processes用来设置工作进程数,events用来设置连接数,http用来设置server节点。
1worker_processes 1;
2
3events {
4 ......
5}
6
7http {
8 ......
9}
1http {
2 server {
3 listen 80;
4 server_name localhost;
5
6 #access_log logs/host.access.log main;
7
8 location / {
9 root html;
10 index index.html index.htm;
11 }
12
13 #error_page 404 /404.html;
14
15 # redirect server error pages to the static page /50x.html
16 #
17 error_page 500 502 503 504 /50x.html;
18 location = /50x.html {
19 root html;
20 }
21
22 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
23 #
24 #location ~ .php$ {
25 # root html;
26 # fastcgi_pass 127.0.0.1:9000;
27 # fastcgi_index index.php;
28 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
29 # include fastcgi_params;
30 #}
31 }
32
33
34 #server {
35 # listen 8000;
36 # listen somename:8080;
37 # server_name somename alias another.alias;
38
39 # location / {
40 # root html;
41 # index index.html index.htm;
42 # }
43 #}
44
45
46 # HTTPS server
47 #
48 #server {
49 # listen 443 ssl;
50 # server_name localhost;
51
52 # ssl_certificate cert.pem;
53 # ssl_certificate_key cert.key;
54
55 # ssl_session_cache shared:SSL:1m;
56 # ssl_session_timeout 5m;
57
58 # ssl_ciphers HIGH:!aNULL:!MD5;
59 # ssl_prefer_server_ciphers on;
60
61 # location / {
62 # root html;
63 # index index.html index.htm;
64 # }
65 #}
66}
获取Nginx需要的证书与秘钥
现在大多数开发使用的是Spring Boot,做为开发的基础,对于一个Java的项目,一般使用的证书是jks证书或者是pkcs12,如果要使用Nginx来开启SSL,当我们需要自己制作证书的时候,就需要把JKS的证书转换成PKCS12的格式,然后通过openssl提取证书和私钥。需要证书是.crt和私钥是.key。
1.生成JKS格式秘钥库
1keytool -genkeypair -alias springcert -keyalg RSA -keysize 2048 -keypass 123456 -validity 3650 -keystore d:springcert.jks -storepass 123456
2.将jks秘钥库转为p12秘钥库
p12秘钥库是PKCS12格式的证书库,标准的格式,现在也是keytool和Java官方推荐的,在jdk1.7之前,都是推荐的JKS格式秘钥库。
1keytool -importkeystore -srckeystore d:springcert.jks -srcalias tomcat -destkeystore d:springcert.p12 -deststoretype PKCS12
2# 查看新格式(pkcs12)证书库keytool -deststoretype PKCS12 -keystore newkeystore.p12 -list
3.将jks秘钥库转为p12秘钥库
1# 将p12转换成crt证书
2openssl pkcs12 -in d:springcert.p12 -nokeys -clcerts -out d:springcert.crt
3# 将p12生成非加密的key
4openssl pkcs12 -in d:springcert.p12 -nocerts -nodes -out d:springcert.key
Nginx开启SSL配置
1 # HTTPS server
2 server {
3 listen 443 ssl;
4 server_name localhost 127.0.0.1;
5
6 ssl_certificate d://springcert.crt;
7 ssl_certificate_key d://springcert.key;
8
9 ssl_session_cache shared:SSL:1m;
10 ssl_session_timeout 5m;
11
12 ssl_ciphers HIGH:!aNULL:!MD5;
13 ssl_prefer_server_ciphers on;
14
15 location / {
16 #root html;
17 root C://learn-soft//nginx-1.18.0//mypags;
18 index index.html index.htm;
19 }
20 }
Http请求强制转换成为Https请求
上述虽然是可以进行https的方式访问了,但是如果我们仍然使用http的方式请求,就会报Bad Request错误,这个时候我们就要把http请求强转到https请求上面来,解决方式也很简单,如下即可。其实就是把80端口的请求,重定向到https请求上。
1server {
2 listen 80;
3 server_name localhost 127.0.0.1;
4 return 301 https://localhost$request_uri;
5}
上述图片是不支持https时候的情况,就会出现Bad Request提示。因为我们在server之中配置了80监听,所以已经有了http请求处理的方案,但是如果没有https那就会提示不支持https,上述就会把请求转发到https请求上,所以需要配置https处理。
本篇文章来源于微信公众号: 疾风小虎牙
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/13866.html