# Nginx 安装
# 第一步:下载 nginx 压缩包
在这里可以去 nginx 官网下载->点我下载 nginx (opens new window) 也可以直接使用 wget 命令下载,指令如下所示(请根据自己的需求进行下载):
wget -c https://nginx.org/download/nginx-1.10.1.tar.gz
注意:这一步最好在自己的目标目录进行操作,我一般是把压缩包下载到/usr/local 目录下。
# 第二步:配置 nginx 安装所需的环境
- 安装 gcc
安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境。安装指令如下:
yum install gcc-c++
- 安装 PCRE pcre-devel
Nginx 的 Rewrite 模块和 HTTP 核心模块会使用到 PCRE 正则表达式语法。这里需要安装两个安装包 pcre 和 pcre-devel。第一个安装包提供编译版本的库,而第二个提供开发阶段的头文件和编译项目的源代码。安装指令如下:
yum install -y pcre pcre-devel
- 安装 zlib
zlib 库提供了开发人员的压缩算法,在 Nginx 的各种模块中需要使用 gzip 压缩。安装指令如下:
yum install -y zlib zlib-devel
- 安装 Open SSL
nginx 不仅支持 http 协议,还支持 https(即在 ssl 协议上传输 http),如果使用了 https,需要安装 OpenSSL 库。安装指令如下:
yum install -y openssl openssl-devel
# 第三步:解压 nginx 压缩包并安装
将压缩包进行解压,解压指令还是写一下吧:
tar -zxvf nginx-1.10.1.tar.gz
解压之后,进入加压文件,即 cd nginx-1.10.1。 然后进行配置,推荐使用默认配置,直接./configure 就好了,如下图所示:
额外说明:如果需要开始 https 支持,这里请不要直接执行./configure,即不要直接执行该脚本,而是在该脚本后面加上 SSL 模块,请执行如下命令替代 ./confingure :
./configure --with-http_ssl_module
至于 --with-http_ssl_module 的意思,顾名思义可知是添加 https 支持。 添加 https 支持也很简单,添加 SSL 证书并修改 nginx.conf 配置文件即可,如何修改不做介绍,自行百度~ 附带一个 SSL 证书免费申请网站
# 第四步:编译安装 nginx
这里和 redis 的编译安装比较类似,首先在当前目录(/usr/local/nginx-1.10.1)进行编译。输入 make 即可
make
然后回车,如果编译出错,请检查是否前面的 4 个安装都没有问题。 编译成功之后,就可以安装了,输入以下指令:
make install
ok,安装成功。 这时候返回上一级目录,就会发现多了 nginx 目录,接下来,启动 nginx。
# 第五步:启动 nginx
进入/usr/local/nginx/sbin 目录,输入./nginx 即可启动 nginx
./nginx
关闭 nginx
./nginx -s quit 或者 ./nginx -s stop
重启 nginx
./nginx -s reload
查看 nginx 进程
ps aux|grep nginx
设置 nginx 开机启动,只需在 rc.local 增加启动代码即可。
vim /etc/rc.local
然后在底部增加/usr/local/nginx/sbin/nginx
此外,进入/usr/local/nginx/conf 目录可修改 nginx 的配置文件 -> vim nginx.conf 譬如修改域名以及端口啥的,在 server 里面进行修改
nginx.conf
user root;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
client_header_buffer_size 32K;
large_client_header_buffers 4 64K;
include /etc/nginx/conf.d/*.conf;
}
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
/etc/nginx/conf.d/example.conf
server {
listen 9000;
server_name 192.168.220.46;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location /portal/ {
alias /data/product/micro_snm/portal/;
}
location /portal_gx/ {
alias /data/product/micro_gx/portal/;
}
location /portal2/ {
alias /data/product/micro_snm/portal2/;
}
location /portal_separation/ {
alias /data/product/micro_snm/portal_separation/;
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
}
location /cms-picture/ {
alias /data/product/micro_snm/var/cms-picture/;
}
location /cms-upload/ {
alias /data/product/micro_snm/var/cms-upload/;
}
location /var/ {
alias /data/product/micro_snm/var/;
}
location /commons/ {
proxy_pass http://192.168.220.46:9001/commons/;
client_max_body_size 9999999m;
proxy_redirect off;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /cs_app/ {
proxy_pass http://192.168.220.46:9017/cs_app/;
client_max_body_size 9999999m;
proxy_redirect off;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /cs_iam/ {
#proxy_pass http://192.168.220.46:9002/cs_iam/;
#9033 is new iam
proxy_pass http://192.168.220.46:9033/cs_iam/;
proxy_redirect off;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_buffering off;
}
location /cs_iam_gx/ {
proxy_pass http://192.168.220.46:9202/cs_iam/;
proxy_cookie_path /cs_iam /cs_iam_gx;
proxy_redirect off;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /cs_cms/ {
proxy_pass http://192.168.220.46:9003/cs_cms/;
proxy_redirect off;
client_max_body_size 20m;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_buffering off;
}
location /toolkit/ {
proxy_pass http://192.168.220.46:9008/toolkit/;
proxy_connect_timeout 1200;
proxy_send_timeout 1200;
proxy_read_timeout 1200;
proxy_redirect off;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /cs_c1/ {
proxy_pass http://192.168.220.46:9030/cs_c1/;
client_max_body_size 9999999m;
proxy_redirect off;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /usergroup/ {
proxy_pass http://192.168.220.46:6060/usergroup/;
client_max_body_size 9999999m;
proxy_connect_timeout 1200;
proxy_send_timeout 1200;
proxy_read_timeout 1200;
proxy_redirect off;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#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;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157