nginx配置

nginx配置

安装

1
2
ubuntu
apt-get install nginx

命令

  1. 启动

    1
    nginx
  2. 重新加载配置文件

    1
    nginx -s reload
  3. 停止

    1
    nginx -s stop

目录

  1. nginx.conf: /etc/nginx/nginx.conf
  2. log: /var/log/nginx/access.log

https设置

  1. 证书申请

  2. 一些注意事项

    1. 云厂商安全组配置相应入站端口规则
    2. 二级域名也需要添加至dns解析中
    3. dv型ssl证书二级域名页需要单独申请
  3. nginx配置

    1. 将相应证书放置于/etc/nginx目录中
    2. nginx.conf添加443端口配置
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    server {
    listen 443;
    server_name www.linhey.com; #填写绑定证书的域名
    ssl on;
    ssl_certificate 1_www.linhey.com_bundle.crt;
    ssl_certificate_key 2_www.linhey.com.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;#按照这个套件配置
    ssl_prefer_server_ciphers on;
    location / {
    proxy_redirect off;
    proxy_pass http://localhost:3000;
    }
    }

http自动跳转https

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
server{
listen 80;
server_name www.linhey.com;
# 转向对应的https链接
rewrite ^(.*) https://$server_name$request_uri;
}

server {
listen 443;
server_name www.linhey.com;
ssl on;
ssl_certificate 1_www.linhey.com_bundle.crt;
ssl_certificate_key 2_www.linhey.com.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
location / {
proxy_redirect off;
proxy_pass http://localhost:3000;
}
}

静态资源配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
server {
listen 443;
server_name www.linhey.com;
# 多个 location 依次匹配,直至匹配成功.

location /JSBridge {
# alias响应的路径:配置路径+静态文件(去除location中配置的路径)
# 查询路径: /root/html/JSBridge
alias /root/html/JSBridge;
autoindex on;
}

location /blog {
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
# 优先加载索引文件
index index.html;
# root响应的路径:配置的路径+完整访问路径(完整的location配置路径+静态文件)
# 查询路径: /root/html/blog
root /root/html;
}
}

端口映射

1
2
3
4
5
6
7
8
9
server {
listen 443;
server_name www.linhey.com;
location / {
proxy_redirect off;
# 映射本地端口
proxy_pass http://localhost:3000;
}
}

多端口映射

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
server {
listen 443;
server_name www.linhey.com;
location / {
proxy_redirect off;
# 映射本地端口
proxy_pass http://localhost:3000;
}
}

server {
listen 443;
server_name api.linhey.com;
location / {
proxy_redirect off;
# 映射本地端口
proxy_pass http://localhost:3001;
}
}

error

1.Permission denied

1
user nginx; => user root;