Eli's Blog

1. 添加nginx用户

1
2
3
groupadd nginx  

useradd -g nginx -s /sbin/nologin -M nginx

2. 安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
wget http://openresty.org/download/openresty-1.15.8.2.tar.gz

tar zxvf openresty-1.15.8.2.tar.gz

cd openresty-1.15.8.2

# 解决openssl支撑包问题, 删除.openssl
vi bundle/nginx-1.15.8/auto/lib/openssl/conf

CORE_INCS="$CORE_INCS $OPENSSL/.openssl/include"
CORE_DEPS="$CORE_DEPS $OPENSSL/.openssl/include/openssl/ssl.h"
CORE_LIBS="$CORE_LIBS $OPENSSL/.openssl/lib/libssl.a"
CORE_LIBS="$CORE_LIBS $OPENSSL/.openssl/lib/libcrypto.a"

./configure -j2 --prefix=/usr/local/openresty --with-openssl=/usr/local/openssl

make
make install

vi ~/.bash_profile
export PATH=/usr/local/openresty/nginx/sbin:/usr/local/openresty/bin:$PATH

3. 开机启动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
vi /usr/lib/systemd/system/nginx.service

Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/openresty/nginx/logs/nginx.pid
ExecStartPre=/usr/local/openresty/nginx/sbin/nginx -t -c /usr/local/openresty/nginx/conf/nginx.conf
ExecStart=/usr/local/openresty/nginx/sbin/nginx -c /usr/local/openresty/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

4. 管理ngnix

1
2
3
systemctl enable nginx.service

systemctl start nginx

5. 配置

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
mkdir -p /usr/local/openresty/nginx/conf.d

vi /usr/local/openresty/nginx/conf/nginx.conf
http {
# 去除注释
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'$request_time $upstream_response_time';

...

# 节点末尾添加
include /usr/local/openresty/nginx/conf.d/*.conf;
}

vi /usr/local/openresty/nginx/conf.d/test.conf
server {
listen 80;
server_name 192.168.1.21;
access_log /var/log/nginx/embo.access.log main;
error_log /var/log/nginx/embo.error.log;
error_page 404 = /404;
error_page 403 = /403;
location / {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
#include proxy_params;
proxy_pass http://127.0.0.1:20002/;
proxy_redirect off;
}

location = / {
root /data/www/lp-web/;
index index.html;
}
location ~* \.(html|ico)$ {
root /data/www/lp-web/;
}
}

6. nginx相关命令

1
2
3
4
5
6
7
nginx

nginx -t

nginx -s stop

nginx -s reload

7. 安装问题

7.1 PCRE未安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
checking for SA_RESTART ... found
+ ngx_stream_lua_module was configured
checking for PCRE library ... not found
checking for PCRE library in /usr/local/ ... not found
checking for PCRE library in /usr/include/pcre/ ... not found
checking for PCRE library in /usr/pkg/ ... not found
checking for PCRE library in /opt/local/ ... not found

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

ERROR: failed to run command: sh ./configure --prefix=/usr/local/openresty/nginx \...
1
yum -y install pcre-devel

8. systemctl启动服务,日志异常

1
2
3
4
5
Dec 09 10:19:11 ip-172-31-12-17.cn-northwest-1.compute.internal systemd[1]: Starting nginx.service...
Dec 09 10:19:11 ip-172-31-12-17.cn-northwest-1.compute.internal nginx[26980]: nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
Dec 09 10:19:11 ip-172-31-12-17.cn-northwest-1.compute.internal nginx[26980]: nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
Dec 09 10:19:11 ip-172-31-12-17.cn-northwest-1.compute.internal systemd[1]: Failed to read PID from file /usr/local/openresty/nginx/logs/nginx.pid: Invalid argument
Dec 09 10:19:11 ip-172-31-12-17.cn-northwest-1.compute.internal systemd[1]: Started nginx.service.

原因:启动成功瞬间,暂未找到nginx.pid文件

解决:增加睡眠时间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
vi /usr/lib/systemd/system/nginx.service

Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/openresty/nginx/logs/nginx.pid
ExecStartPre=/usr/local/openresty/nginx/sbin/nginx -t -c /usr/local/openresty/nginx/conf/nginx.conf
ExecStart=/usr/local/openresty/nginx/sbin/nginx -c /usr/local/openresty/nginx/conf/nginx.conf
ExecStartPost=/bin/sleep 0.1 # 新增项
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target
1
2
3
4
5
systemctl daemon-reload

systemctl restart nginx.service

systemctl status nginx

 上一页

hexo