Blog of Jeff Xue

  • Home

  • Categories

  • 读书

  • 编程

  • 思考

  • Archives

  • Tags

ngx_http_headers_module模块使用说明

Posted on 2016-03-22 Edited on 2020-01-19 In 运维

ngx_http_headers_module 允许添加Expires和Cache-Control域和任意的域到响应头中。

例子

1
2
3
4
5
6
7
8
expires    24h;
expires modified +24h;
expires @24h;
expires 0;
expires -1;
expires epoch;
expires $expires;
add_header Cache-Control private;

add_header指令

Syntax: add_header name value [always];
Default: —
Context: http, server, location, if in location

当请求响应码为200,201,204,206,301,302,303,304或307时,该配置的域将会添加到应答头中,可以有多个add_header指令。这些指令均会继承上一层的,除非当前没有使用add_header

expires指令

Syntax: expires [modified] time;
expires epoch | max | off;
Default: expires off;
Context: http, server, location, if in location

该指令为在应答头中添加了Expires和Cache-Control域。
后面为负数,将添加头:Cache-Control: no-cache
后面为正数/0,将添加头:Cache-Control: max-age=t

可以根据不同文件类型设置不同的超时时间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
map $sent_http_content_type  $expires {
"~*image/*" 7d;
"~*application/x-javascript" 7d;
"~*text/css" 7d;
"~*text/javascript" 7d;
"~*application/javascript" 7d;
"~*text/plain" 1d;
"~*application/x-shockwave-flash" 7d;
"~*video/x-flv" 7d;
"~*application/pdf" 7d;
default off;
}

expires $expires;

ngx_http_gzip_module模块使用说明

Posted on 2016-03-22 Edited on 2020-01-19 In 运维

ngx_http_gzip_module是一个使用gzip方法压缩响应内容的过滤器。这会减少一一半以上的传输内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 开启gzip
gzip on;
# 设置用于压缩的buffer数量和大小
gzip_buffers 4 16k;
# 设置进行压缩的请求协议
gzip_http_version 1.1;
# 设置压缩比率 1~9,1为最小化压缩(处理速度快),9为最大化压缩(处理速度慢)
gzip_comp_level 6;
# 设置需要压缩的MIME类型,text/html总是会被压缩
gzip_types text/plain text/php text/xml text/css text/javascript application/javascript application/xhtml+xml application/xml application/rss+xml application/atom_xml application/x-javascript application/x-httpd-php image/svg+xml;
# 指定不需要gzip压缩的浏览器
gzip_disable "msie6"
# 启用应答头Vary:Accept-Encoding
gzip_vary on;

nginx授权资源跨站点访问

Posted on 2016-03-22 Edited on 2020-01-19 In 运维

Access-Control-Allow-Origin 是html5 添加的新功能。基本上, 这是一个http的header, 用在返回资源的时候, 指定这个资源可以被哪些网域跨站访问。

比方说, 你的图片都放在 res.byneil.com 这个域下, 如果在返回的头中没有设置Access-Control-Allow-Origin , 那么别的域是不能外链你的图片的。当然这要取决于浏览器的实现是否遵守规范。所以导致一些网站资源加载不进来.

解决方法就是 在资源的头中 加入 Access-Control-Allow-Origin 指定你授权的域. 这里指定星号 * , 任何域都可以访问我的资源.

1
Access-Control-Allow-Origin: *
Read more »

nginx日志切割

Posted on 2016-03-22 Edited on 2020-01-19 In 运维

由于nginx没有像apache一样自带日志切割工具(rotatelogs),但可以使用linux系统自带的工具logrotate,并配置定时任务即可实现定时日志切割

logrotate日志管理工具

logrotate是一个日志文件管理工具。用来把旧文件轮转、压缩、删除,并且创建新的日志文件。我们可以根据日志文件的大小、天数等来转储,便于对日志文件管理,一般都是通过cron计划任务来完成的。

命令参数说明

logrotate –help

1
2
3
4
5
6
7
8
9
10
Usage: logrotate [OPTION...] <configfile>
-d, --debug Don't do anything, just test (implies -v)
-f, --force Force file rotation
-m, --mail=command Command to send mail (instead of `/usr/bin/mail')
-s, --state=statefile Path of state file
-v, --verbose Display messages during rotation

Help options:
-?, --help Show this help message
--usage Display brief usage message
Read more »

nginx_ssl配置说明

Posted on 2016-03-22 Edited on 2020-01-19 In 运维

HTTPS

本文主要针对nginx支持HTTPS配置进行说明,详情见官方文档

nginx.conf

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
# 服务器证书, nginx无法设置CA证书,导致手机端无法通过ssl握手,把CA证书合并到该文件中即可
ssl_certificate /usr/local/nginx/conf/newssl/nginx_domain.crt;
# 服务器秘钥
ssl_certificate_key /usr/local/nginx/conf/newssl/your.key;
# 设置加密算法套件,不同的加密算法可能会导致不同的漏洞,根据https://linux.cn/article-5374-1.html 修改而来的ciphers
ssl_ciphers ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4;
# 当使用SSLv3和TLS协议时,首选服务器的加密套件
ssl_prefer_server_ciphers on;
# 设置支持的协议,SSLv2 是不安全的,所以我们需要禁用它。我们也禁用 SSLv3,因为 TLS 1.0 在遭受到降级攻击时,会允许攻击者强制连接使用 SSLv3
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
# 不存储session到cache
ssl_session_cache none;
# 关闭session tickets
ssl_session_tickets off;
# 关闭验证客户端证书
ssl_verify_client off;

server {
# 监听端口443 https协议
listen 443 ssl;
# 访问域名
server_name example.com;
# 启用https
ssl on;
# 日志文件
access_log log/access_ssl.log main;
# 默认访问
location / {
proxy_pass http://127.0.0.1:8080;
}
}

nginx1.8基本配置说明

Posted on 2016-03-22 Edited on 2020-01-19 In 运维

nginx.conf

nginx主要的配置文件为nginx.conf,以下配置主要针对该配置文件进行说明

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# 配置运行用户
#user nobody;
# 设置启动进程数量
worker_processes 1;

# 全局错误日志及pid文件
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;

# Provides the configuration file context in which the directives that affect connection processing are specified.
events {
# 指定使用的连接处理方式,详情见:http://nginx.org/en/docs/events.html
# use epoll;
# 每个进程的最大并发连接数
worker_connections 1024;
}


http {
# 设置mime类型
include mime.types;
# 设置默认的mime类型
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;

# 配置是否运行使用sendfile()
sendfile on;
# Enables or disables the use of the TCP_NOPUSH socket option on FreeBSD or the TCP_CORK socket option on Linux.
#tcp_nopush on;

# 设置keepalive超时时间
#keepalive_timeout 0;
keepalive_timeout 65;

# 是否开启gzip压缩
#gzip on;

# 虚拟主机配置
server {
# 监听端口
listen 80;
# 访问域名
server_name localhost;

# 添加指定字符集到“Content-Type”头中
#charset koi8-r;

# 设置本虚拟主机日志
#access_log logs/host.access.log main;

# 默认请求处理
location / {
# 主目录
root html;
# 主页类型
index index.html index.htm;
}

# 定制404页面
#error_page 404 /404.html;

# 重定向50x错误到定制页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

# 转发php脚本到apache进行解析
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# 转发php脚本到fastcgi服务进行解析
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# 禁止访问.htaccess
#location ~ /\.ht {
# deny all;
#}
}


# 另一个虚拟主机配置,设置混合的IP,名称和端口
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;

# location / {
# root html;
# index index.html index.htm;
# }
#}

# HTTPS 服务
#server {
# listen 443 ssl;
# server_name localhost;

# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;

# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;

# location / {
# root html;
# index index.html index.htm;
# }
#}

}

nginx连接处理方式说明

Posted on 2016-03-22 Edited on 2020-01-19 In 运维

连接处理方式

nginx支持多种连接处理方式,具体使用哪一种由当前平台决定。如果当前平台支持多种方式,nginx会自动选择最佳的方式。然而,在有需要的情况下,可以通过use命令来指定连接处理方式。

以下为所支持的连接处理方式:

  • select

    standard method. The supporting module is built automatically on platforms that lack more efficient methods. The –with-select_module and –without-select_module configuration parameters can be used to forcibly enable or disable the build of this module.

  • poll

    standard method. The supporting module is built automatically on platforms that lack more efficient methods. The –with-poll_module and –without-poll_module configuration parameters can be used to forcibly enable or disable the build of this module.

  • kqueue

    efficient method used on FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0, and Mac OS X.

  • epoll

    efficient method used on Linux 2.6+.
    Some older distributions like SuSE 8.2 provide patches that add epoll support to 2.4 kernels.

  • /dev/poll

    efficient method used on Solaris 7 11/99+, HP/UX 11.22+ (eventport), IRIX 6.5.15+, and Tru64 UNIX 5.1A+.

  • eventport

    event ports, efficient method used on Solaris 10.

nginx1.8编译安装

Posted on 2016-03-22 Edited on 2020-01-19 In 运维

概述

nginx [engine x] 是一个HTTP反向代理服务器,一个邮件代理服务器,一个通用的TCP/UDP代理服务器
官方文档

编译安装

1
2
3
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --with-pcre=/usr/local/pcre-8.10 --with-openssl=/usr/local/openssl-1.0.1p --with-zlib=/usr/local/zlib-1.2.7

make && make install

–with-http_ssl_module #支持ssl
–with-http_stub_status_module #支持status
–with-http_gzip_static_module #支持gzip
–with-http_realip_module #允许从请求标头更改客户端的IP地址值
–with-pcre=/usr/local/pcre-8.10 #设置pcre源目录
–with-openssl=/usr/local/openssl-1.0.1p #设置openssl源目录
–with-zlib=/usr/local/zlib-1.2.7 #设置zlib源目录

Read more »

apache使用mod_rewrite模块

Posted on 2016-03-10 Edited on 2020-01-19 In 运维

mod_rewrite

mod_rewrite使用基于规则的重写引擎,基于PCRE正则表达式解析器,动态重写请求的URLs。默认的情况下,mod_write只是映射一个URL到文件系统路径。然而,它可以将一个URL重定向到另一个URL,或者调用一个内部代理。

修改httpd.conf以开启mod_rewrite模块,并在对应的目录中进行配置,详细可看官方文档

1
2
3
4
5
6
7
8
9
10
11
LoadModule rewrite_module modules/mod_rewrite.so

<Directory "/usr/local/apache/htdocs">
# 要支持每个目录的rewrites,需要支持FollowSymLinks
Options Indexes FollowSymLinks
# 开启动态重写
RewriteEngine On
# 可以在.htaccess中为每个目录设置rewrite规则,需将AllowOverride配置为All
AllowOverride All
Require all granted
</Directory>
Read more »

apache的MPMs详解

Posted on 2016-03-10 Edited on 2020-01-19 In 运维

MPMs

Multi-Processing Modules(MPMs)为多路处理模块,对于类UNIX系统,有三个不同的MPM可选择,不同的模式会影响apache的速度和可靠性。

Perfork MPM

该MPM实现了一个非线程,pre-forking的web server,每个进程均可处理请求,并且有一个父进程管理server poll。适用于没有线程安全库,需要避免线程兼容性问题的系统。每个请求之间都是相互独立,这对于那些需要隔离请求的系统来说是最好的MPM。

这个MPM具有很强的自我调节能力,一般情况很少去调整配置。最重要的配置是MaxRequestWorkers,应该让该值大于你所预估的同时并发请求数,同时也应考虑是否有足够的内存。

Read more »
1…678…11
Jeff Xue

Jeff Xue

Think Bigger, Start Simple!
104 posts
10 categories
48 tags
GitHub
© 2016 – 2021 Jeff Xue
Powered by Hexo v3.9.0
|
Theme – NexT.Gemini v7.3.0