nginx配置与开发

配置

rewrite regex replacement [flag];
rewrite的含义:该指令是实现URL重写的指令。
regex的含义:用于匹配URI的正则表达式。
replacement:将regex正则匹配到的内容替换成 replacement。
flag: flag标记。

flag有如下值:

  • last: 本条规则匹配完成后,继续向下匹配新的location URI 规则。(不常用)
  • break: 本条规则匹配完成即终止,不再匹配后面的任何规则(不常用)。
  • redirect: 返回302临时重定向,浏览器地址会显示跳转新的URL地址。
  • permanent: 返回301永久重定向。浏览器地址会显示跳转新的URL地址。

比如rewrite ^/(.*) http://www.baidu.com/$1 permanent;
说明:

  • rewrite 为固定关键字,表示开始进行rewrite匹配规则。
  • regex 为 ^/(.*)。 这是一个正则表达式,匹配完整的域名和后面的路径地址。
  • replacement就是 http://www.baidu.com/1 这块了,其中1是取regex部分()里面的内容。如果匹配成功后跳转到的URL。
  • flag 就是 permanent,代表永久重定向的含义,即跳转到 http://www.baidu.com/$1 地址上。

证书和私钥的生成。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
1.创建服务器证书密钥文件 server.key:
openssl genrsa -des3 -out server.key 2048
输入密码,确认密码,自己随便定义,但是要记住,后面会用到。
2.创建服务器证书的申请文件 server.csr
openssl req -new -key server.key -out server.csr
输出内容为:
Enter pass phrase for root.key: ← 输入前面创建的密码
Country Name (2 letter code) [AU]:CN ← 国家代号,中国输入CN
State or Province Name (full name) [Some-State]:BeiJing ← 省的全名,拼音
Locality Name (eg, city) []:BeiJing ← 市的全名,拼音
Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名
Organizational Unit Name (eg, section) []: ← 可以不输入
Common Name (eg, YOUR name) []: ← 输入域名,如:iot.conet.com
Email Address []:admin@mycompany.com ← 电子邮箱,可随意填
Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []: ← 可以不输入
An optional company name []: ← 可以不输入
4.备份一份服务器密钥文件
cp server.key server.key.org
5.去除文件口令
openssl rsa -in server.key.org -out server.key
6.生成证书文件server.crt
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

配置示例如下。

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
worker_processes 1;
events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;

server {
listen 443 ssl;
server_name localhost;
ssl_certificate /etc/nginx/ssl_pem/server.crt;
ssl_certificate_key /etc/nginx/ssl_pem/server.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers off; # on;

location /sdk/config {
rewrite ^/sdk/config$ /sdk/$arg_app.cfg; # $arg_app代表/sdk/config?app=test后面的test参数
}

location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
# https://docs.nginx.com/nginx/admin-guide/web-server/serving-static-content/
location /download/ {
autoindex on; # nginx -V没有编译这个模块就不会生效,不加这个会默认访问index.html文件
alias /usr/share/nginx/html/download/bin/;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}

Sets the root directory for requests. For example, with the following configuration
location /i/ {
root /data/w3;
}

Defines a replacement for the specified location. For example, with the following configuration
location /i/ {
alias /data/w3/images/;
}
当访问/i/top.gif时,root是去/data/w3/i/top.gif请求文件,alias是去/data/w3/images/top.gif请求

当有域名设置了cname时,cname的这个域名也要设置server,并且需要证书,只是cname的记录值不是ip地址了,注意要添加@记录,相当于没有www的情况。

1
server_name dyhlyb.nephen.cn dyhlyb.com www.dyhlyb.com;


使用cerbot自动生成证书。

1
2
3
4
5
6
7
8
# Create a self signed certificate, should the user need it
openssl req -subj "/C=$COUNTRY/" -x509 -nodes -newkey rsa:2048 -keyout /etc/ssl/private/default-server.key -out /etc/ssl/certs/default-server.crt

# https://www.nginx.com/blog/using-free-ssltls-certificates-from-lets-encrypt-with-nginx/
certbot --nginx --non-interactive --agree-tos -m $EMAIL --domains $DOMAINS

# 定期更新
certbot renew --quiet

nginx反向代理django.

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
server {
listen 8080 default_server;
listen [::]:8080 default_server;

charset utf-8;

location /static {
alias /nephen/lyb-work/static;
}

location / {
include /nephen/lyb-work/conf/uwsgi_params;
uwsgi_pass unix:/nephen/lyb-work/log/uwsgi.sock;
}
}

location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:8080;

proxy_set_header REMOTE_ADDR $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
real_ip_recursive on;
}

使用docker进行部署启动, docker exec -ti nginx bash, nginx -s reload,或者docker restart nginx-config。
curl -X POST -k https://XXX

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 启动时设置 --rm 选项,这样在容器退出时就能够自动清理容器内部的文件系统
# -d: 后台运行容器,并返回容器ID;
# -i: 以交互模式运行容器,通常与 -t 同时使用;
# -p: 指定端口映射,格式为:主机(宿主)端口:
# -t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用;
# --name="nginx-lb": 为容器指定一个名称;
# -e username="ritchie": 设置环境变量;
docker run -dit -p 443:443 -p 80:80 --name nginx-config \
-v /mnt/hgfs/Tmp/html:/usr/share/nginx/html \
-v /home/nginx/ssl_pem:/etc/nginx/ssl_pem \
-v /home/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:rw \
-v /home/nginx/logs:/var/log/nginx \
--restart unless-stopped \
nginx

nephen wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!