添加header
server {
listen 80;
server_name localhost;
location ^~ /demo {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header userid '123456789';
proxy_set_header X-Nginx-Proxy true;
proxy_pass http://10.10.10.25:443;
}
}
————————————————
版权声明:本文为优快云博主「不知名工程师某某某」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.youkuaiyun.com/jeccisnd/article/details/87888979
https://blog.youkuaiyun.com/gaoshanqr/article/details/79303135
https://www.cnblogs.com/wshenjin/p/13071392.html
https://www.cnblogs.com/jiaoyiping/p/5906257.html
http://nginx.org/en/docs/http/ngx_http_core_module.html#variables
http://blog.phpdr.net/nginx%E9%85%8D%E7%BD%AE%E4%B8%AD%E8%8E%B7%E5%8F%96get%E5%8F%82%E6%95%B0.html
$arg_PARAMETER这个变量值为:GET请求中变量名PARAMETER参数的值。
$args 这个变量等于GET请求中的所有参数。
nginx优化之请求直接返回json数据 - duanxz - 博客园
对于有些服务端接口返回是固定值的json,可通过配置nginx直接返回json,减少程序的加载对资源的占用,减少接口响应时间
location ~* (request/update)$ {
default_type application/json;
return 200 '{"update":"no"}';
}
记得加default_type application/json ,不然浏览器打开时,是个文件。
nginx优化之请求直接返回json数据 - duanxz - 博客园
nginx配置返回文本或json - 小得盈满 - 博客园
固定文本:
location ~ ^/get_text { default_type text/html; return 200 'This is text!'; }
固定json:
location ~ ^/get_json { default_type application/json; return 200 '{"status":"success","result":"nginx json"}'; }
保存后重新载入配置即可生效,注意:default_type必须要添加,否则浏览器会当成不识别的文件进行下载
另外也可以简单的根据请求的URL返回不同的字符串,示例如下:
location ~ ^/get_text/article/(.*)_(\d+).html$ { default_type text/html; set $s $1; set $d $2; return 200 str:$s$d; }
这样可以简单截取url中的字符串,当然也可以用(.*)匹配所有的,实际中根据不同需求定义即可
上面的就是一些简单的案例,在服务器环境中对于简单的处理,充分使用nginx可以省去一些编程工作
另外补充一下中文显示的问题,因为Linux下采用的是utf-8的字符编码,默认情况下我们的浏览器在服务器没有指定编码或者静态页面没有声明编码的情况下会以GBK的编码去渲染页面,这样默认情况下返回中文的话浏览器用gbk来解析utf-8编码,显然会出现乱码,这时要在nginx location块中主动添加header来输出正确编码,添加内容为: add_header Content-Type 'text/html; charset=utf-8'; 这样浏览器就知道我们使用的是哪种编码了,如下图:
或者把add_header这行换成 charset utf-8; 也是可以的