Doraemon

小叮当    2012 - 2023
Doraemon

Choose mode

  • dark
  • auto
  • light
首页
Category
  • 前端开发
  • 后端
  • 数据库
  • 运维
Tag
TimeLine
关于
  • 关于我
Contact
  • GitHub
author-avatar

小叮当

39

Article

25

Tag

首页
Category
  • 前端开发
  • 后端
  • 数据库
  • 运维
Tag
TimeLine
关于
  • 关于我
Contact
  • GitHub

Nginx-Windows 的安装和配置

小叮当    2012 - 2023

Nginx-Windows 的安装和配置


小叮当 2020-08-09 Nginx运维

常用命令:

重新加载配置文件:nginx -s reload

快速停止 nginx:nginx -s stop

完整有序的停止 nginx:nginx -s quit

杀 nginx 进程:taskkill taskkill /f /t /im nginx.exe

# 1、下载 nginx for windows,并解压到程序目录

官网下载列表:http://nginx.org/en/download.html

直接下载 nginx-1.16.0.zip

# 2、启动 nginx

方法一、直接双击 nginx.exe

方法二、在 nginx 目录打开cmd输入命令 nginx.exe 或者 start nginx ,回车即可

方法三(生产环境推荐使用此方法)、将 Nginx 设置为 Windows 服务

工具:Windows Service Wrapper(winsw.exe),下载地址 。GitHub 地址:https://github.com/kohsuke/winsw

基本使用命令如下:

  • To install a service, run myapp.exe install
  • To start a service, run myapp.exe start
  • To stop a service, run myapp.exe stop
  • To restart a service, run myapp.exe restart
  • To uninstall a service, run myapp.exe uninstall

将winsw-2.2.0-net4.exe文件下载,放在 Nginx 的根目录,重命名为nginx-service.exe

创建文件nginx-service.xml并且填入如下内容:

<?xml version="1.0" encoding="UTF-8" ?>
<service>
  <id>nginx</id>
  <name>nginx</name>
  <description>nginx</description>
  <executable>D:/Program Files/nginx-1.12.2/nginx.exe</executable>
  <startargument>-p</startargument>
  <startargument>D:/Program Files/nginx-1.12.2</startargument>
  <logpath>D:/Program Files/nginx-1.12.2/logs</logpath>
  <logmode>roll</logmode>
  <stopexecutable>D:/Program Files/nginx-1.12.2/nginx.exe</stopexecutable>
  <stopargument>-p</stopargument>
  <stopargument>D:/Program Files/nginx-1.12.2</stopargument>
  <stopargument>-s</stopargument>
  <stopargument>stop</stopargument>
  <stoptimeout>6sec</stoptimeout>
</service>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

创建文件nginx-service.exe.config并且填入如下内容(tips,正常情况下不需要此文件):

<configuration>
  <startup>
	<supportedRuntime version="v2.0.50727" />
	<supportedRuntime version="v4.0" />
  </startup>
  <runtime>
	<generatePublisherEvidence enabled="false"/>
  </runtime>
</configuration>
1
2
3
4
5
6
7
8
9

使用如下命令安装,成功后可在 service 中查看到名为 nginx 的服务

nginx-service.exe install
1

# 3、检查 nginx 是否启动成功

浏览器地址栏输入网址 http://localhost

# 4、端口占用排查

  • 检查 80 端口是否被占用:netstat -ano | findstr 0.0.0.0:80

或netstat -ano | findstr "80"

  • tips:基本上停止 IIS 80 端口站点就不会出现 80 端口占用的情况了

# 5、nginx 之反向代理

nginx 的配置文件是 conf 目录下的 nginx.conf,默认配置的 nginx 监听的端口为 80,如果 80 端口被占用可以修改为未被占用的端口即可。

当我们修改了 nginx 的配置文件 nginx.conf 时,不需要关闭 nginx 后重新启动 nginx,只需要执行命令nginx -s reload 即可让改动生效。

例子:通过 proxy_pass 配置请求转发地址,即当我们依然输入http://localhost/test 时,请求会跳转到我们配置的服务器(http://localhost:9000/)

location /test/ {
    proxy_pass   http://localhost:9000/;
	proxy_set_header        Host $host;
    proxy_set_header        X-Real-IP $remote_addr;  #获取真实ip
    proxy_set_header REMOTE-HOST $remote_addr;
    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;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;#获取代理者的真实ip
    proxy_redirect          off;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Windows 安装 Nginx 参考地址:https://www.cnblogs.com/jiangwangxiang/p/8481661.html

让 Nginx 作为 Windows 服务开机自启动:https://blog.csdn.net/zhengun/article/details/84825320

Windows 下将 nginx 配置成服务并设置开机自启动:https://blog.csdn.net/liutong123987/article/details/79112707

https://blog.csdn.net/h610443955/article/details/81096506

# 6、nginx 服务器安装 SSL 证书

https://help.aliyun.com/knowledge_detail/95491.html

修改配置文件nginx.conf,指定 ssl 证书路径,监听 443 端口。执行命令nginx -s quit退出 Nginx,再重新启动

server {
    listen       443 ssl;
    server_name  www.bbb.com;
    resolver 114.114.114.114 223.5.5.5 valid=3600s;
    #ssl on;
    ssl_certificate      cert/1_www.bbb.com.crt;
    ssl_certificate_key  cert/2_www.bbb.com.key;
    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;
    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;

    #charset koi8-r;
    #access_log  logs/$host.access.log  main;
    location / {
        proxy_pass   http://www.bbb.com:8080;
        index  index.html index.htm;
    }
}

生产环境

server {
    server {
    listen  443 ssl;
    server_name  www.bbb.ink;

    #自动添加www
    if ($host != 'www.bbb.ink' ) {
	rewrite ^/(.*)$ https://www.bbb.ink/$1 permanent;
    }

	ssl_certificate   C://cert//a.pem;
	ssl_certificate_key  C://cert//a.key;
	ssl_session_cache    shared:SSL:1m;
	ssl_session_timeout  5m;

	ssl_ciphers  HIGH:!aNULL:!MD5;
	ssl_prefer_server_ciphers  on;
    #charset koi8-r;

    #access_log  logs/host.access.log  main;

   location / {
        root D:/WWW/demo; #定义服务器的默认网站根目录位置
        index  index.html index.htm; #定义首页索引文件的名称
		try_files $uri $uri/ /index.html; #解决路由404问题
    }

    error_page  404  /index.html; #解决刷新出现404页面
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}

# 7、bugfix:启动 nginx 报错,An attempt was made to access a socket in a way forbidden

情况一:运行 cmd, 输入netstat -aon|findstr "443"找到 0.0.0.0:443的 PID,在任务管理器结束进程。 vmware-hostd.exe

情况二:把 Routing and Remote Access 服务停掉,重启 nginx 好了.

# 8、自动跳转到 HTTPS

server {
    listen 80;
    server_name google.0513c.site;
    rewrite ^(.*)$ https://$host$1 permanent; #访问http跳转至https
}

# 9、bugfix:git 提交 push 到远程时出现 error: RPC failed; HTTP 413 curl 22

打开 nginx.conf 中的 http 配置段中加入client_max_body_size 100m;重启 nginx

# 10、Nginx 配置 gzip

http {
    ...
    gzip on;
    gzip_disable "msie6"; #配置禁用gzip条件,支持正则。此处表示ie6及以下不启用gzip(因为ie低版本不支持)

    gzip_vary on; #选择支持vary header;改选项可以让前端的缓存服务器缓存经过gzip压缩的页面; 这个可以不写,表示在传送数据时,给客户端说明我使用了gzip压缩
    # gzip_proxied any;
    gzip_comp_level 6; #设置压缩比率,最小为1,处理速度快,传输速度慢;9为最大压缩比,处理速度慢,传输速度快; 这里表示压缩级别,可以是0到9中的任一个,级别越高,压缩就越小,节省了带宽资源,但同时也消耗CPU资源,所以一般折中为6
    gzip_buffers 16 8k; #设置压缩缓冲区大小,此处设置为4个16K内存作为压缩结果流缓存****
    gzip_http_version 1.1; #压缩版本

    #gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; #js压缩失效,因为缺失application/javascript

    gzip_types    text/xml text/plain text/css application/javascript application/x-javascript application/rss+xml;     # 哪些文件可以被压缩
    gzip_disable    "MSIE [1-6]\.";     # IE6无效
    ...

    #bugfix:git 提交 push 到远程时出现 error: RPC failed; HTTP 413 curl 22
    client_max_body_size 1024M;
    client_body_buffer_size 10M; # 干什么用的?

    include /etc/nginx/conf.d/*.conf;
	include /etc/nginx/sites-enabled/*;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

# 11、nginx批处理文件nginx.bat

@echo off
rem 提供Windows下nginx的启动,重启,关闭功能

echo ==================begin========================

cls
::ngxin 所在的盘符
set NGINX_PATH=E:

::nginx 所在目录
set NGINX_DIR=E:\nginx-1.15.2\
color 0a
TITLE Nginx 管理程序增强版

CLS

echo.
echo. ** Nginx 管理程序  ***
echo. *** create 2018-08-17 ***
echo.

:MENU

echo. ***** nginx 进程list ******
::tasklist|findstr /i "nginx.exe"
tasklist /fi "imagename eq nginx.exe"

echo.

    if ERRORLEVEL 1 (
        echo nginx.exe不存在
    ) else (
        echo nginx.exe存在
    )

echo.
::*************************************************************************************************************
echo.
	echo.  [1] 启动Nginx
	echo.  [2] 关闭Nginx
	echo.  [3] 重启Nginx
	echo.  [4] 刷新控制台
	echo.  [5] 重新加载Nginx配置文件
	echo.  [6] 检查测试nginx配置文件
	echo.  [7] 查看nginx version
	echo.  [0] 退 出
echo.

echo.请输入选择的序号:
set /p ID=
	IF "%id%"=="1" GOTO start
	IF "%id%"=="2" GOTO stop
	IF "%id%"=="3" GOTO restart
	IF "%id%"=="4" GOTO MENU
	IF "%id%"=="5" GOTO reloadConf
	IF "%id%"=="6" GOTO checkConf
	IF "%id%"=="7" GOTO showVersion
	IF "%id%"=="0" EXIT
PAUSE

::*************************************************************************************************************
::启动
:start
	call :startNginx
	GOTO MENU

::停止
:stop
	call :shutdownNginx
	GOTO MENU

::重启
:restart
	call :shutdownNginx
	call :startNginx
	GOTO MENU

::检查测试配置文件
:checkConf
	call :checkConfNginx
	GOTO MENU

::重新加载Nginx配置文件
:reloadConf
    call :checkConfNginx
	call :reloadConfNginx
	GOTO MENU

::显示nginx版本
:showVersion
    call :showVersionNginx
	GOTO MENU


::*************************************************************************************
::底层
::*************************************************************************************
:shutdownNginx
	echo.
	echo.关闭Nginx......
	taskkill /F /IM nginx.exe > nul
	echo.OK,关闭所有nginx 进程
	goto :eof

:startNginx
	echo.
	echo.启动Nginx......
	IF NOT EXIST "%NGINX_DIR%nginx.exe" (
        echo "%NGINX_DIR%nginx.exe"不存在
        goto :eof
     )

	%NGINX_PATH%
	cd "%NGINX_DIR%"

	IF EXIST "%NGINX_DIR%nginx.exe" (
		echo "start '' nginx.exe"
		start "" nginx.exe
	)
	echo.OK
	goto :eof


:checkConfNginx
	echo.
	echo.检查测试 nginx 配置文件......
	IF NOT EXIST "%NGINX_DIR%nginx.exe" (
        echo "%NGINX_DIR%nginx.exe"不存在
        goto :eof
     )

	%NGINX_PATH%
	cd "%NGINX_DIR%"
	nginx -t -c conf/nginx.conf

	goto :eof

::重新加载 nginx 配置文件
:reloadConfNginx
	echo.
	echo.重新加载 nginx 配置文件......
	IF NOT EXIST "%NGINX_DIR%nginx.exe" (
        echo "%NGINX_DIR%nginx.exe"不存在
        goto :eof
     )

	%NGINX_PATH%
	cd "%NGINX_DIR%"
	nginx -s reload

	goto :eof

::显示nginx版本
:showVersionNginx
	echo.
	%NGINX_PATH%
	cd "%NGINX_DIR%"
	nginx -V
 	goto :eof

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160

# 12、server 配置

参考地址https://www.cnblogs.com/Laggage/p/11660853.html

server {
    #侦听80端口
    listen       80;
    #定义使用www.xx.com访问
    server_name  www.xx.com;
    #设定本虚拟主机的访问日志
    access_log  logs/www.xx.com.access.log  main;
    #默认请求
    location / {
          root   /root;      #定义服务器的默认网站根目录位置
          index index.php index.html index.htm;   #定义首页索引文件的名称
          fastcgi_pass  www.xx.com;
          fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name;
          include /etc/nginx/fastcgi_params;
    }
    # 定义错误提示页面
    error_page   500 502 503 504 /50x.html;
        location = /50x.html {
        root   /root;
    }
    #静态文件,nginx自己处理
    location ~ ^/(images|javascript|js|css|flash|media|static)/ {
        root /var/www/virtual/htdocs;
        #过期30天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。
        expires 30d;
    }
    #PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置.
    location ~ \.php$ {
        root /root;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /home/www/www$fastcgi_script_name;
        include fastcgi_params;
    }
    #设定查看Nginx状态的地址
    location /NginxStatus {
        stub_status            on;
        access_log              on;
        auth_basic              "NginxStatus";
        auth_basic_user_file  conf/htpasswd;
    }
    # 配置文件下载服务
    location /download {
        root /www;
        # 让浏览器打开下载窗口,关键在于attachment,attachment后面不可以加冒号
        add_header Content-Disposition 'attachment';
        add_header Content-Type: 'APPLICATION/OCTET-STREAM';
    }
    #禁止访问 .htxxx 文件
    location ~ /\.ht {
        deny all;
    }
 }
}
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

# 13、Nginx 基础使用说明

  • 情况一
location /proxy/ {
     proxy_pass http://127.0.0.1:81/;
}
1
2
3

会被代理到http://127.0.0.1:81/index.html

  • 情况二
location /proxy/ {
proxy_pass http://127.0.0.1:81;
}
1
2
3

会被代理到http://127.0.0.1:81/proxy/index.html

  • 情况三
location /proxy/ {
proxy_pass http://127.0.0.1:81/test/;
}
1
2
3

会被代理到http://127.0.0.1:81/test/index.html

  • 情况四(TODO:有疑问)
location /proxy/ {
proxy_pass http://127.0.0.1:81/test;
}
1
2
3

会被代理到http://127.0.0.1:81/test/index.html

  • 1、下载 nginx for windows,并解压到程序目录
  • 2、启动 nginx
  • 3、检查 nginx 是否启动成功
  • 4、端口占用排查
  • 5、nginx 之反向代理
  • 6、nginx 服务器安装 SSL 证书
  • 7、bugfix:启动 nginx 报错,An attempt was made to access a socket in a way forbidden
  • 8、自动跳转到 HTTPS
  • 9、bugfix:git 提交 push 到远程时出现 error: RPC failed; HTTP 413 curl 22
  • 10、Nginx 配置 gzip
  • 11、nginx批处理文件nginx.bat
  • 12、server 配置
  • 13、Nginx 基础使用说明