配置结构

常用配置
- 配置 https
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;}}
- 配置端口转发
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;}}}
- 跨域问题解决
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/;}
- 配置压缩
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
- 负载均衡
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;}
常用命令
# 重新打开日志文件nginx -s reopen# 重新加载配置文件nginx -s reload# 查看nginx的配置文件的目录,测试配置文件是否正确nginx -t# 停止ngix(优雅关闭)nginx -s quit# 关闭 nginx(快速关闭,不管有没有请求)nginx -s stop
常见错误
invalid PID
- 错误信息
nginx: [error] invalid PID number "" in "/usr/local/var/run/nginx
- 解决方法
$ sudo nginx -c /usr/local/etc/nginx/nginx.conf$ sudo nginx -s reload
nginx做转发时,带’_’的header内容丢失
- 解决方法
underscores_in_headers on;
例子
worker_processes 1; //开启进程数小于CPU数error_log logs/error.log; //自定义错误日志保存位置,全局设置,默认logs/error.logevents {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和端口,可以配置多个serverserver {listen 80; //监听端口,可以是ip:port 或者 portserver_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地址}}
