直接返回文件内容
1 2 3 4 5 6 7 8 9 10
| location ^~ /get_json_file{ default_type application/json; add_header Content-Type 'application/json; charset=utf-8'; alias D:/resouces/code/java/test.json; }
location ~ ^/get_json { default_type application/json; return 200 '{"status":"success","result":"hello world!"}'; }
|
负载均衡
- 配置
conf/nginx.conf
- 默认轮询 添加下列配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| http{ .... upstream tomcats { server 127.0.0.1:8080 server 127.0.0.1:9080 } ... } server{ ... location / { proxy_pass http://tomcats; } }
|
错误记录
重载配置 ./nginx -c ../conf/nginx.conf -s reload
, 报错: nginx: [emerg] unknown directive
.
1
| 原因是因为因为nginx.conf配置被windows下的记事本编辑过, 编码变成了UTF-8+BOOM, nginx 报错.
|
reload 不生效
浏览器端禁用缓存, 可发起新连接
http 转发至 https
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
| http { resolver 8.8.8.8; }
server { listen 443 ssl; server_name xxx.com; ssl on; root html; index index.html index.htm; ssl_certificate cert/xxx.pem; ssl_certificate_key cert/xxx.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 = / { proxy_set_header Host $http_host; proxy_redirect off; proxy_pass http://xxx/xxx/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 1000m; } }
service { listen 80; server_name xxx.xxx.com;
location / { if ($request_method ~ ^(POST|DELETE|OPTIONS)$) { proxy_pass https://$host; break ; } rewrite ^(.*)$ https://$host$1 permanent; } }
|