nginx默认是对客户端使用长连接,对服务器使用短连接。

centos7操作系统要事先优化。

Nginx官网提供了三个类型的版本

Mainline version:Mainline 是 Nginx 目前主力在做的版本,可以说是开发版

Stable version:最新稳定版,生产环境上建议使用的版本

Legacy versions:遗留的老版本的稳定版

#####简单用—屌丝青年

nginx下载安装
http://nginx.org/packages/centos/7/x86_64/RPMS/

rpm -vh http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.12.1-1.el7.ngx.x86_64.rpm

#########系统版 —普通青年

yum -y install yum-priorities epel-release

yum -y install nginx

The default server root directory is /usr/share/nginx/html

#########官方版—-文艺青年

Stable version 最新稳定版

1
2
3
4
5
6
7
8
9
cat >> /etc/yum.repos.d/nginx-Stable.repo <<EOF
[nginx]
name=nginx Stable repo
baseurl=http://nginx.org/packages/centos/7/\$basearch/
gpgcheck=0
enabled=1
EOF

或者rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

Mainline version 开发版

1
2
3
4
5
6
7
8
9
cat >>/etc/yum.repos.d/nginx-Mainline.repo <<EOF
[nginx]
name=nginx Mainline repo
baseurl=http://nginx.org/packages/mainline/centos/7/\$basearch/
gpgcheck=0
enabled=1
EOF
#####
~~~

1
2
3
rpm --import https://nginx.org/keys/nginx_signing.key

yum -y install nginx

########源码安装—专业青年

1
2
3
4
5
6
7
8
9
10
11
12
13
yum install –y gcc gcc-c++ autoconf automake wget unzip zlib zlib-devel pcre pcre-devel openssl openssl-devel  libxml2 libxml2-devel  libxslt   libxslt-devel gd-devel perl-devel perl-ExtUtils-Embed

groupadd nginx
useradd -M -s /sbin/nologin nginx -g nginx
mkdir -p /data/logs/nginx
chown -R nginx.nginx /data/logs/nginx
mkdir -p /opt/nginx/conf/conf.d/
mkdir -p /var/cache/nginx/
chown -R nginx.nginx /var/cache/nginx/
路径约定/opt
wget https://nginx.org/download/nginx-1.12.1.tar.gz
tar zxvf /opt/nginx-1.12.1.tar.gz
cd /opt/nginx-1.12.1

(默认没有带第三方模块)

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
./configure \
--prefix=/opt/nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-cc-opt=-DTCP_FASTOPEN=23 \
--with-pcre \
--with-compat\
--user=nginx\
--group=nginx\
--with-file-aio\
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--with-http_addition_module\
--with-http_sub_module\
--with-http_dav_module\
--with-http_mp4_module\
--with-http_gunzip_module\
--with-http_gzip_static_module\
--with-http_random_index_module \
--with-http_stub_status_module\
--with-http_auth_request_module \
--with-http_slice_module \
--with-http_realip_module \
--with-http_secure_link_module \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-stream_ssl_preread_module \
--with-threads \
--with-http_xslt_module=dynamic \
--with-http_image_filter_module=dynamic \
--with-http_perl_module=dynamic\
--with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' \
--with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'


make
make install

chown -R nginx.nginx /data/logs/nginx
chown -R nginx.nginx /opt/nginx/
echo '/opt/nginx/sbin/nginx ' >> /etc/rc.d/rc.local

优化/opt/nginx/conf/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
user  nginx;
worker_processes auto;
error_log /data/logs/nginx/error.log warn;
pid logs/nginx.pid;
worker_rlimit_nofile 655350;

events {
accept_mutex off;
multi_accept on;
use epoll;
worker_connections 655350;
}

http {
include mime.types;
default_type application/octet-stream;
charset utf-8;

log_format main '$time_iso8601 - $status - $bytes_sent - $request_time - $http_x_forwarded_for - $remote_addr - $server_addr - $upstream_addr - '
'$request_method - $scheme://$host$uri?$query_string - $server_protocol - $ssl_protocol - $http_referer - $http_user_agent - $gzip_ratio - $http_cookie ';

log_format json
'{
time_iso8601: $time_iso8601
status: $status
bytes_sent: $bytes_sent
http_host: $http_host
request_time: $request_time
http_x_forwarded_for: $http_x_forwarded_for
remote_addr: $remote_addr
server_addr: $server_addr
upstream_addr: $upstream_addr
request_method: $request_method
scheme: $scheme
host: $host
uri: $uri
query_string: ?$query_string
server_protocol: $server_protocol
ssl_protocol: $ssl_protocol
ssl_cipher: $ssl_cipher
http_referer: $http_referer
http_user_agent: $http_user_agent
gzip_ratio: $gzip_ratio
http_cookie: $http_cookie
}';

access_log /data/logs/nginx/access.log main;
server_names_hash_bucket_size 256;
client_header_buffer_size 256k;
large_client_header_buffers 4 256k;
client_max_body_size 500m;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
server_tokens off;

limit_conn_zone $binary_remote_addr zone=perip:100m;
limit_conn_zone $server_name zone=perserver:100m;

gzip on;
gzip_min_length 1k;
gzip_comp_level 4;
gzip_buffers 4 16k;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/javascript application/json application/x-json;
gzip_http_version 1.1;
gzip_vary off;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";

fastcgi_buffers 32 8k;
fastcgi_buffer_size 8K;

##### enable websocket ######
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}

### for server_name
include /opt/nginx/conf/conf.d/*.conf;

}

~

启动nginx

/opt/nginx/sbin/nginx

查看安装配置

/opt/nginx/sbin/nginx -V