Nginx 相关

nginx 安装新模块

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
#以 OneinStack 安装 nginx-rtmp-module 模块为例子, OneinStack 解压后的目录为 /packages/oneinstack

#1. 下载 nginx-rtmp-module 并解压
git clone https://github.com/arut/nginx-rtmp-module.git

#2. 查看 nginx 已安装的模块
[root@192 ~]# nginx -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
built with OpenSSL 1.1.1a 20 Nov 2018
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module --with-openssl=../openssl-1.1.1a --with-pcre=../pcre-8.42 --with-pcre-jit --with-ld-opt=-ljemalloc

#3. 解压 nginx、openssl-1.1.1a、pcre-8.42
tar -zxvf /packages/oneinstack/src/nginx-1.14.2.tar.gz
tar -zxvf /packages/oneinstack/src/openssl-1.1.1a.tar.gz
tar -zxvf /packages/oneinstack/src/pcre-8.42.tar.gz

#4. 编译 nginx (不要 make install), 这里添加模块 --add-module=/packages/nginx-rtmp-module
cd /packages/oneinstack/src/nginx-1.14.2
./configure --prefix=/usr/local/nginx --user=www --group=www --add-module=/packages/nginx-rtmp-module --with-http_stub_status_module --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module --with-openssl=../openssl-1.1.1a --with-pcre=../pcre-8.42 --with-pcre-jit --with-ld-opt=-ljemalloc
make

#5. 替换 nginx 二进制文件
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
cp ./objs/nginx /usr/local/nginx/sbin/

#6. 重启nginx
systemctl restart nginx

nginx + apache 动静分离

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
#服务器 IP:192.168.1.7

#1. apache 虚拟主机相关配置
<VirtualHost *:88>
DocumentRoot "/data/wwwroot/default"
ServerName 127.0.0.1
ErrorLog "/data/wwwlogs/error_apache.log"
CustomLog "/data/wwwlogs/access_apache.log" common
</VirtualHost>

#2. default 目录下有2个文件, test.html 和 test.php

#3. 配置 apache - conf/httpd.conf, 修改默认端口为 88
Listen 88

#4. 配置 nginx - conf/nginx.conf, server {} 中添加以下代码
access_log /data/logs/access_nginx.log combined;
location / {
try_files $uri @apache;
}
location @apache {
proxy_pass http://127.0.0.1:88;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:88
location ~ \.php$ {
proxy_pass http://127.0.0.1:88;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
expires 30d;
access_log off;
}
location ~ .*\.(js|css)?$ {
expires 7d;
access_log off;
proxy_pass http://127.0.0.1:8080;
}
location ~ /\.ht {
deny all;
}

#5. 访问 192.168.1.7/test.html, 查看 access_apache.log 和 access_nginx.log, 发现只有 access_nginx.log 有日志, 说明已实现静态分离

#6. 访问 192.168.1.7/test.php, 查看 access_apache.log 和 access_nginx.log, 发现两个文件都有新的日志生成, 说明已实现动态分离

nginx 设置反向代理后, js|css 无法加载

1
2
3
4
5
6
7
8
9
#修改对应的项目配置文件, 添加以下代码
server {
location ~ .*\.(js|css)?$ {
expires 7d;
access_log off;
#反向代理地址需与项目配置的一样
proxy_pass http://127.0.0.1:88;
}
}

nginx ssl 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#1. 下载证书相关文件,并重命名为 cert.pem 和 cert.key, 上传至 /usr/local/nginx/conf/cert 目录下

#2. 配置 nginx.conf
server {
listen 443 ssl;
server_name www.xxx.com;

ssl_certificate cert/cert.pem;
ssl_certificate_key cert/cert.key;

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

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

location / {
root /www/test;
index index.html index.htm;
}
}

nginx 支持 php-fpm 模块

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
#1. PHP 编译安装时,需在编译参数后面加上 --enable-fpm 就行, 比如
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php --enable-fpm

#2. 进入 PHP 源码包
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf

#3. vi /usr/local/php/etc/php-fpm.d/www.conf
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
; will be used.
user = apache
group = apache

#4. 启动 php-fpm
/usr/local/php/sbin/php-fpm

#5. 配置 nginx.conf
vi /usr/local/nginx/conf/nginx.conf

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /www/test;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /www/test$fastcgi_script_name;
include fastcgi_params;
}

#6. 重启 nginx
service nginx restart

将 nginx 添加为 service 服务

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
vi /etc/init.d/nginx

#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop()
{
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL

#保存退出后,设置可执行权限
chmod +x /etc/rc.d/init.d/nginx

#添加系统服务
chkconfig --add nginx

Rewrite 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
server {
location / {
root /data/app/default/public;
index index.php index.html index.htm;

if (!-e $request_filename) {
# 重定向
rewrite ^/web/(\d+)?(.*)$ /web/$2 last;
}

if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
}
}
}
0%