nginx操作笔记

介绍

nginx是一款开源轻量级服务器,由俄罗斯人开发,在后端主要作为反向代理服务器,同时也可以配置负载均衡,具有高性能高稳定性的特点。

搭建

centos环境下安装

1
yum install nginx

查看安装路径

1
whereis nginx

常用指令

  • 启动nginx

    1
    nginx
  • 测试配置文件

    1
    nginx -t
  • 重启nginx服务

    1
    nginx -s reload
  • 强制停止nginx服务

    1
    nginx -s stop
  • 优雅关闭nginx服务

    1
    nginx -s quit
  • 查看nginx版本信息

    1
    nginx -v

常用配置

前端项目配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
server {
#监听端口,多个server可以同时监听一个端口
listen 80;
#可以是域名,也可以是ip
server_name masaike.msk.msk.com;
#域名根目录
location / {
# 前端文件根目录
root /opt/manager;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}

}

应用服务器反向代理配置

1
2
3
4
5
6
7
8
9
10
11
12
13
server {
listen 80;
server_name test.masaike.com;
location / {
#请求转发应用服务器地址
proxy_pass http://<内网ip>:8001/;
#写header
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_set_header Connection keep-alive;
}
}

同一域名下多配置

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
server {
listen 80;
server_name hd.masaike.com;

#charset koi8-r;

#access_log logs/host.access.log main;

#子目录之一,此处为静态资源
location /201907 {
alias /opt/student;
index index.html index.htm;
}
#子目录之二,动态请求服务器
location /api/ {
proxy_pass http://172.18.201.159:8000/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_set_header Connection keep-alive;
}

#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 html;
}

https搭建配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
   server {
# 80端口跳转https端口
listen 80;
server_name manager.wolun.dqscq.com;
rewrite ^(.*)$ https://manager.wolun.dqscq.com permanent;
}

server {
listen 443;
server_name manager.wolun.dqscq.com;
ssl on;
ssl_certificate /opt/certs/wolun/ssl/2924361_manager.wolun.dqscq.com.pem;
ssl_certificate_key /opt/certs/wolun/ssl/2924361_manager.wolun.dqscq.com.key;
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 /opt/applications/wolun/platform-frontend/platForm;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}

}