CentOS 安装 Nginx

使用 yum 自动安装 nginx

1
yum -y install 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
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
# vim /usr/local/webserver/nginx/conf/nginx.conf

user root;
worker_processes 1;

error_log /log/nginx/nginx_error.log error; #日志位置和日志级别
worker_rlimit_nofile 1024;

events{
use epoll;
worker_connections 1024;
}


http {

include 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 /log/nginx/access.log main;

keepalive_timeout 0;

gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/javascript text/css application/xml;
gzip_vary on;

server { # 默认重定向到https
listen 80;
server_name xxx.yyy;

return 301 <https://xxx.yyy>;
}

server {
listen 443 ssl;
server_name xxx.yyy;
charset utf-8;

ssl_certificate /home/www/xxx.yyy.pem; # 密钥,由HTTPS证书颁发者提供
ssl_certificate_key /home/www/xxx.yyy.key; # 密钥,由HTTPS证书颁发者提供
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;

location / {
root /home/www/dist/;
index index.html index.htm;
}

}
}


检查配置文件正确性

1
nginx -t

常用命令

1
2
3
4
5
nginx # 启动
nginx -s reload # 重新载入配置文件
nginx -s reopen # 重启 Nginx
nginx -s stop # 停止 Nginx