常用Nginx配置
# 同一端口HTTP跳转HTTPS
在只监听一个端口的情况下,将http访问跳转为https 尝试通用error_page去拦截
error_page 497 https://$host:$server_port$uri$is_args$args;
1
# 测试返回文本
使用application/json
location /hello {
default_type application/json;
return 200 '{"status":"success","result":"nginx json"}';
}
1
2
3
4
2
3
4
# 非默认443端口
使用https协议,不用443端口
listen default
server {
listen 2443 default ssl;
ssl_certificate /ssl/hellossl/hello.ok96.cn.pem;
ssl_certificate_key /ssl/hellossl/hello.ok96.cn.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;
return 404;
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
listen
server {
listen 2443 ssl;
server_name git.ok96.cn;
client_max_body_size 60M;
client_body_buffer_size 512k;
ssl_certificate /ssl/hellossl/hello.ok96.cn.pem;
ssl_certificate_key /ssl/hellossl/hello.ok96.cn.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;
error_page 497 https://$host:$server_port$uri$is_args$args;
location /hello {
default_type application/json;
return 200 '{"status":"success","result":"nginx json"}';
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 端口转发
配置文件与http同级,如下将本机端口7070转发到213服务器的6060端口
stream{
upstream mysqlmaster {
server 192.168.213.213:6060 weight=1;
}
server {
listen 7070;
proxy_upload_rate 102400;
proxy_download_rate 102400;
proxy_pass mysqlmaster;
}
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 同一端口根据不同域名转发
增加两个一样的listen,不同server_name
listen default
server {
listen 443 default ssl;
ssl_certificate /ssl/5499474_ok96.cn.pem;
ssl_certificate_key /ssl/5499474_ok96.cn.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;
return 404;
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
两个listen ,可用不同证书
server {
listen 443 ssl;
server_name ok96.cn;
client_max_body_size 60M;
client_body_buffer_size 512k;
ssl_certificate /ssl/5499474_ok96.cn.pem;
ssl_certificate_key /ssl/5499474_ok96.cn.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 /hello {
default_type application/json;
return 200 '{"status":"success","result":"nginx json"}';
}
}
server {
listen 443 ssl;
server_name hello.ok96.cn;
client_max_body_size 60M;
client_body_buffer_size 512k;
ssl_certificate /ssl/hellossl/hello.ok96.cn.pem;
ssl_certificate_key /ssl/hellossl/hello.ok96.cn.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 /hello {
default_type application/json;
return 200 '{"status":"success","result":"nginx json"}';
}
}
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
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
# http自动跳转https
让80端口全部转发到443
listen default
server {
listen 80 default;
return 404;
}
1
2
3
4
2
3
4
listen
server {
listen 80;
server_name ok96.cn www.ok96.cn hello.ok96.cn;
client_max_body_size 60M;
client_body_buffer_size 512k;
#rewrite ^(.*) https://$server_name$1 permanent;
rewrite ^(.*)$ https://$host$1 permanent;
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
上次更新: 2023/03/13, 03:16:04