Nginx安装与配置(Windows)

Nginx下载

http://nginx.org/en/download.html

运行

打开nginx所在目录

然后就可以执行下面的命令了

1
2
3
4
start nginx           // 启动Nginx
nginx -s stop // 停止nginx
nginx -s reload // 重新加载配置文件
nginx -s quit // 退出nginx

也可以用我写的Nginx管理工具

链接:https://pan.baidu.com/s/1H2143IGnXhNZhaFcmXUWxA
提取码:iw9t

配置文件

引用配置

conf目录下新建一个conf.d文件夹

nginx.conf添加引用

1
2
3
4
http {
# 以上内容省略
include conf.d/*.conf;
}

引用配置的时候路径可以是绝对路径 类似于(注意斜杠的方向)

1
include D:/soft/nginx-1.8.1/conf/conf.d/*.conf;

也可以是

1
include conf.d/*.conf;

上面的配置要在http内部

但是以下两种是错误

1
include ./conf.d/*.conf;

1
include /conf.d/*.conf;

配置示例

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
upstream auth_psvmc {
server 192.168.0.214:8080;
ip_hash;
}

server {
listen 80;
server_name auth.psvmc.cn;
rewrite ^/auth/(.*)$ /$1 last;
location / {
proxy_pass http://auth_psvmc/auth/;
proxy_cookie_path /auth/ /;
proxy_redirect /auth/ /;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
}

配置示例(SSL/Alias)

证书文件放在Nginx根目录下的cert文件夹中

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
upstream schoolfile_xhkjedu {
server 127.0.0.1:8908;
}

server {
listen 80;
server_name schoolfile.xhkjedu.com;
listen 443;
ssl on;
ssl_certificate 'E:/Program Files/nginx-1.12.2/cert/xhkjedu.pem';
ssl_certificate_key 'E:/Program Files/nginx-1.12.2/cert/xhkjedu.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;
charset utf-8;

location /download/ {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Content-Type' 'application/octet-stream';
add_header 'Content-Disposition' 'attachment';
alias E:/data/wwwjarapi/schoolfile/static/;
}

location ~* /static {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
root E:/data/wwwjarapi/schoolfile/;
}

location / {
proxy_pass http://schoolfile_xhkjedu/;
proxy_cookie_path / /;
proxy_redirect / /;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 1000m;
client_body_buffer_size 128k;
client_body_timeout 5m;
proxy_connect_timeout 300s;
proxy_send_timeout 300s;
proxy_read_timeout 300s;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_buffer_size 64k;
proxy_buffers 8 64k;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 128k;
send_timeout 20s;
}
}

配置注意项

  1. 路径中的斜杠一定要是/,默认复制的路径是反斜杠,一定要更换一下
  2. 路径中不能有空格,如果有空格整个路径要用单引号('')引一下
  3. 路径中不能用.././

错误解决

我在配置后 报了如下错误

could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32

解决方式

nginx.conf中的http块中添加

1
server_names_hash_bucket_size 64;

扩展

http_image_filter_module

这个模块在Windows环境中不支持,还没找到解决方法。