替换

  • sub_filter指令: sub_filter string(原字符串) replacement(用于替换的字符串);

用于设置需要使用说明字符串替换说明字符串.string是要被替换的字符串,replacement是 新的字符串,它里面可以带变量。

  • sub_filter_last_modified指令: sub_filter_last_modified on | off;

用于设置网页内替换后是否修改 可在nginx.conf的 http, server, location三个位置配置使 用,默认值是off;

  • sub_filter_once指令:sub_filter_once on | off;

用于设置字符串替换次数,默认只替换一次。如果是on,默认只替换第一次匹配到的到字 符,如果是off,那么所有匹配到的字符都会被替换;

  • sub_filter_types指令:sub_filter_types *

用于指定需要被替换的MIME类型,默认为“text/html”,如果制定为*,那么所有的;
说明:以上指令可在nginx.conf的http, server, location三个位置配置使用;
示例

location / {
sub_filter "http://127.0.0.1:8110" "https://aaa.bbb.com" ;
sub_filter_once off;
sub_filter_types application/atom+xml;
sub_filter_last_modified on;

proxy_pass http://127.0.0.1:8110;
}

限制并发

同时限制ip和虚拟主机最大并发连接

http {
limit_conn_zone $binary_remote_addr zone=perip:10m;
limit_conn_zone $server_name zone=perserver:10m;
server {
location / {
limit_conn perip 10;
limit_conn perserver 1000;
limit_conn addr 5; #是限制每个IP只能发起5个连接
limit_rate 100k; #限速为 100KB/秒
}
}
}

隐藏服务器信息

移除X-AspNet-Version

只需要在Web.config中增加这个配置节

<httpRuntime enableVersionHeader="false"/>

移除nginx版本号

http{
server_tokens off;
}

移除X-AspNetMvc-Version

MvcHandler.DisableMvcResponseHeader =true;

支持与请求端建立keepalive

http {
keepalive_timeout 120s 120s;
keepalive_requests 10000;
}

支持与负载后端的keepalive

keepalive详解

upstream s1{
server 172.26.154.20:8090 ;
keepalive 1000; #重点需要添加
}
server{
listen 4010;
access_log logs/ai.log json;
#location /s1/ {
location ~* ^\/(.*)\/* {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://$1;
proxy_http_version 1.1; ## 重点需要添加
proxy_set_header Connection ""; ##重点需要添加
}
}

防止出现status 499

http{
proxy_ignore_client_abort on;#代理服务端不要主要主动关闭客户端连接。
}

设置不缓存

add_header Cache-Control no-store;

支持tsl1.2

配置ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

注意 所有conf里的ssl配置都要改成这个 否则还是tsl1.0

支持websocket

http{
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
}


location / {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
# 这是配置webpysessoin丢失的问题
fastcgi_param SCRIPT_NAME "";
}

设置后端超时时间

fastcgi_connect_timeout 300;        
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;

默认配置

 default_type  application/octet-stream;


server_names_hash_bucket_size 64;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
log_format json '{"@timestamp":"$time_iso8601",'
'"server_host":"$server_addr",'
'"remote_addr":"$remote_addr",'
'"remote_user":"$remote_user",'
'"request":"$request",'
'"http_user_agent":"$http_user_agent",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"url":"$uri",'
'"domain":"$host",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"status":"$status"}';
#access_log logs/access.log main;
include ext/*.conf;
sendfile on;
#tcp_nopush on;

add_header X-Frame-Options DENY;
server_tokens off;
#keepalive_timeout 0;
keepalive_timeout 65;


large_client_header_buffers 4 16k;
client_max_body_size 30m;
client_body_buffer_size 128k;

#FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。下面参数看字面意思都能理解。
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
#gzip模块设置
gzip on; #开启gzip压缩输出
gzip_min_length 1k; #最小压缩文件大小
gzip_buffers 4 16k; #压缩缓冲区
gzip_http_version 1.0; #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
gzip_comp_level 2; #压缩等级
gzip_types text/plain text/javascript application/x-javascript text/css application/xml;

更改路径转发后端代理

例如 前端地址是 http://localhost/a/b/c
后端服务器应该访问的地址是 /b/c

 location /a/ {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://server/;
}
upstream server{
server 47.104.237.193:10006;
}