配置结构

常用配置

  1. 配置 https
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
server {
listen 443;
server_name localhost;
ssl on;
root html;
index index.html index.htm;
ssl_certificate cert/1535407297197.pem;
ssl_certificate_key cert/1535407297197.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 html;
index index.html index.htm;
}
}
  1. 配置端口转发
1
2
3
4
5
6
7
8
9
10
11
12
http{
proxy_read_timeout 300s;
proxy_send_timeout 300s;
keepalive_requests 1000;
keepalive_timeout 300s;
server{
listen 8888;
location / {
proxy_pass http://192.168.1.20:8080;
}
}
}
  1. 跨域问题解决
1
2
3
4
5
6
7
8
9
10
11
server {
listen 8081;//前端调试打开localhost:8081页面;js文件中后台接口访问localhost:8081/data;这样就保证不跨域了
server_name localhost;
access_log logs/host.access.log main;
location / { //访问localhost:8081实际上访问是前端端口http://localhost:8080/
proxy_pass http://localhost:8080/;
}
location ^~ /data {//访问localhost:8081/data实际上访问是后端接口http://10.128.166.42:8533/
 rewrite ^/data/(.*) /$1 break;
proxy_pass http://10.128.166.42:8533/;
}
  1. 配置压缩
1
2
3
4
5
6
7
8
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss image/jpeg image/gif image/png
  1. 负载均衡
1
2
3
4
5
6
7
8
9
10
upstream favtomcat {
server 10.0.6.108:7080 weight=5;
server 10.0.0.85:8980 weight=5;
}

location / {
root html;
index index.html index.htm;
proxy_pass http://favtomcat;
}

常用命令

1
2
3
4
5
6
7
8
9
10
# 重新打开日志文件
nginx -s reopen
# 重新加载配置文件
nginx -s reload
# 查看nginx的配置文件的目录,测试配置文件是否正确
nginx -t
# 停止ngix(优雅关闭)
nginx -s quit
# 关闭 nginx(快速关闭,不管有没有请求)
nginx -s stop

常见错误

  1. invalid PID

    • 错误信息
1
nginx: [error] invalid PID number "" in "/usr/local/var/run/nginx
  • 解决方法
1
2
$ sudo nginx -c /usr/local/etc/nginx/nginx.conf
$ sudo nginx -s reload
  1. nginx 做转发时,带’_‘的 header 内容丢失

    • 解决方法
1
underscores_in_headers on;

例子

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

worker_processes 1; //开启进程数小于CPU数
error_log logs/error.log; //自定义错误日志保存位置,全局设置,默认logs/error.log
events {
worker_connections 1024; //每个进程最大连接数(最大连接=连接数x进程数)每个worker允许同时产生多少个链接,默认1024
}

http {
include mime.types; //文件扩展名与文件类型映射表
default_type application/octet-stream; //默认文件类型
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' //自定义日志文件输出格式 全局设置
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main; 自定义全局请求日志保存位置,全局设置,默认logs/access.log, 定义格式:文件存储位置 + 日志输出格式
sendfile on; //打开发送文件
keepalive_timeout 0; //连接超时时间
keepalive_timeout 65;
gzip on; //打开gzip压缩
配置虚拟主机,基于域名、ip和端口,可以配置多个server
server {
listen 80; //监听端口,可以是ip:port 或者 port
server_name 10.128.166.57; //监听域名,可以是ip或者域名,server_name有三种匹配方式:精准匹配(www.domain.com)、通配符匹配(*.domain.com 、www.*)、正则表达式匹配(~^(?.+)\.domain\.com$)
access_log logs/host.access.log main; //自定义请求日志,局部,当前server有效
error_page 500 502 503 504 /50x.html; //错误页面及其返回地址
charset UTF-8; //设置字符集
location / { //当访问10.128.166.57:80时
proxy_pass http://10.128.166.57:80:8083; //实际上访问的时http://10.128.166.57:80:8083地址
}
location ^~/data { //当访问10.128.166.57:80/data时
proxy_pass http://10.128.166.57:80:8084; //实际上访问的时http://10.128.166.57:80:8084地址
}

}

参考

  1. 从一份配置清单详解 Nginx 服务器配置

本文地址 https://blog.coder4j.cn/posts/59e56e3a/