如题

down nginx Stable version

1
2
[root@jdu4e00u53f7 soft]# wget https://nginx.org/download/nginx-1.12.2.tar.gz
[root@jdu4e00u53f7 soft]# tar -xzvf nginx-1.12.2.tar.gz

下载最新版本,请查看官网:http://nginx.org/en/download.html
记得需要下载源码版本,即 .tar.gz 版本,而不是针对 windows 的 .zip 版本。

安装依赖

1
yum -y install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
  • 编译依赖 gcc 环境
  • PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。
  • zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。
  • OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
    • nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。

安装依赖时可能会遇到冲突

  • 2019-03-31 追加
    提示:
    1
    2
    Protected multilib versions: pcre-8.32-17.el7.x86_64 != pcre-8.32-15.el7_2.1.i686
    Error: Protected multilib versions: zlib-1.2.7-18.el7.x86_64 != zlib-1.2.7-15.el7.i686

解决

加参数 --setopt=protected_multilib=false

1
yum -y install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel --setopt=protected_multilib=false

./configure

1
2
3
4
5
6
7
8
9
# tar -xzvf nginx-1.14.0.tar.gz
# cd nginx-1.14.0
[root@jdu4e00u53f7 nginx-1.12.2]# ./configure
checking for OS
+ Linux 3.10.0-514.el7.x86_64 x86_64
checking for C compiler ... found
+ using GNU C compiler
+ gcc version: 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
...

为了支持 ssl 需要如此安装

1
2
3
4
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module

nginx 站点配置
rewrite ^(.*)$ https://$host$1 permanent; #用于将http页面重定向到https页面

make

1
2
3
[root@jdu4e00u53f7 nginx-1.12.2]# make && make install
...
make[1]: Leaving directory `/soft/nginx-1.12.2'

查看安装目录

1
2
[root@jdu4e00u53f7 nginx-1.12.2]# whereis nginx
nginx: /usr/local/nginx

修改 nginx 配置文件

1
2
3
4
5
6
7
8
9
10
mkdir -p /usr/local/nginx/vhosts
vi /usr/local/nginx/conf/nginx.conf

#user nobody;
#改为:
user root;

# 在最后一行 "}" 前面添加:
# include ../vhosts/*.conf; # 相对路径
include /usr/local/nginx/vhosts/*.conf; # 用全路径

配置站点

vim /usr/local/nginx/vhosts/web.conf

1
2
3
4
5
6
7
8
9
10
11
12
server {
listen 80;
server_name kq.saleonline.top;
charset utf-8;
access_log logs/kq.saleonline.top.access.log;
error_log logs/kq.saleonline.top.error.log;

location / {
root /soft-app/defaultsite/;
index index.html;
}
}

启动

以 root 身份操作,下同。

1
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

检查配置是否正确

1
/usr/local/nginx/sbin/nginx -t

重启 nginx(重新加载配置文件)

1
/usr/local/nginx/sbin/nginx -s reload

停止 nginx,快速关闭

1
/usr/local/nginx/sbin/nginx -s stop

停止 nginx,优雅的关闭

1
/usr/local/nginx/sbin/nginx -s quit

重启日志文件

1
/usr/local/nginx/sbin/nginx -s reopen

开机启动

编辑服务文件

1
vim /lib/systemd/system/nginx.service

内容如下

1
2
3
4
5
6
7
8
9
10
11
12
13
[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

设置开机启动

1
2
[root@AndyCentOS7Basic ~]# systemctl enable nginx.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

常用命令

设置 nginx 开机启动

1
systemctl enable nginx.service

停止开机自启动

1
systemctl disable nginx.service

启动 nginx 服务

1
systemctl start nginx.service

查看服务当前状态

1
systemctl status nginx.service

重新启动服务

1
systemctl restart nginx.service

查看所有已启动的服务

1
systemctl list-units --type=service

权限问题

1
2018/05/02 15:06:32 [error] 8541#0: *15 "/home/unginx/apps/dz/index.php" is forbidden (13: Permission denied), client: 117.139.199.91, server: club.aipeigo.com, request: "GET / HTTP/1.1", host: "club.aipeigo.com"

修改 nginx 启动用户
按如下操作:

1
2
3
4
5
vim /usr/local/nginx/conf/nginx.conf

#user nobody;
#改为:
user root;

301 302 重定向

301

1
2
3
4
5
server {
listen 80;
server_name aipeigo.com www.aipeigo.com;
rewrite ^(.*)$ http://www.hellojiazhang.com$1 permanent;
}

302

1
2
3
4
5
server {
listen 80;
server_name www.hellojiazhang.net hellojiazhang.net;
rewrite ^/(.*) http://www.hellojiazhang.com$1 redirect;
}

测试 301 重定向是否成功

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@izwz97mqm1h3u2vxe4kesjz siteConfigs]# curl -I aipeigo.com
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.2
Date: Thu, 28 Jun 2018 06:33:55 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://www.hellojiazhang.com/

[root@izwz97mqm1h3u2vxe4kesjz siteConfigs]# curl -I www.aipeigo.com
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.2
Date: Thu, 28 Jun 2018 06:34:07 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://www.hellojiazhang.com/

也可以直接访问需要重定向的域名,成功之后会跳转至新域名。
还可以在站长工具上做测试:站长工具 > HTTP状态查询

查看 nginx 版本

1
2
[utomcat@centOS7BasicForTest ~]$ /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.14.0

设置超时时间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
server {
listen 80;
server_name domain.com;
charset utf-8;
access_log logs/domain.com.access.log;
error_log logs/domain.com.error.log;
root /appHome/;
index index.php;

location ~ [^/].php(/|$) {
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
}
}

默认站点,可用服务器的 IP 地址直接访问

只需要在 listen 80 后添加 default,如:

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
server {
listen 80 default;
# gzip config
gzip on;
gzip_min_length 1k;
gzip_comp_level 9;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";

server_name dev.mis.com;
charset utf-8;
access_log logs/dev.mis.com.access.log;
error_log logs/dev.mis.com.error.log;
root /home/utomcat/apps/dev/frontend;

location / {
index index.html;
# 用于配合 browserHistory使用
try_files $uri $uri/ /index.html;
}

location /api {
proxy_pass http://localhost:8080/api;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
}
}

以上是以 ant design pro 为模板开发的一个项目打包后的网站配置文件。

默认站点只允许设置一个

否则重新启动会报错

1
2
[root@localhost vhosts]# /usr/local/nginx/sbin/nginx -s reload
nginx: [emerg] a duplicate default server for 0.0.0.0:80 in /usr/local/nginx/vhosts/web-dev.conf:2

检查配置也会提示相应错误信息:

1
2
3
[root@localhost vhosts]# /usr/local/nginx/sbin/nginx -t
nginx: [emerg] a duplicate default server for 0.0.0.0:80 in /usr/local/nginx/vhosts/web-dev.conf:2
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed