服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

云服务器|WEB服务器|FTP服务器|邮件服务器|虚拟主机|服务器安全|DNS服务器|服务器知识|Nginx|IIS|Tomcat|

服务器之家 - 服务器技术 - Nginx - Nginx在Windows下的安装与使用过程详解

Nginx在Windows下的安装与使用过程详解

2023-05-17 13:52芸灵fly Nginx

Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器,这篇文章主要介绍了Nginx在Windows下的安装与使用,需要的朋友可以参考下

说明

Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。在连接高并发的情况下,Nginx是Apache服务器不错的替代品:Nginx在美国是做虚拟主机生意的老板们经常选择的软件平台之一。能够支持高达 50,000 个并发连接数的响应。笔者将会使用Nginx将默认网址使用的80端口与Tomcat使用的8080端口进行对接,实现使用80端口(域名)访问Tomcat下的网页,并配置HTTPS协议提高安全性。

下载

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

Nginx在Windows下的安装与使用过程详解

选择合适的Windows版本进行下载,笔者以稳定版1.12.2版本为例,解压后如下图:

Nginx在Windows下的安装与使用过程详解

安装

1 准备:

首先需要域名和SSL证书来配置HTTPS协议,SSL证书可以从很多地方获取或者自己创建,笔者以腾讯云的CA证书为例:

有了域名和SSL证书后,按照腾讯云的官方提示,将配置Nginx的安全证书(.crt)和注册表项(.key)放到Nginx解压目录下的conf文件夹下方便管理(证书的名字可以自己修改,笔者将其改为1.crt和2.key)

Nginx在Windows下的安装与使用过程详解

然后需要将其配置到Nginx中,修改conf目录下的nginx.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
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
#user  nobody;
#user root;
worker_processes  1;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;
events {
worker_connections  1024;
}
http {
include       mime.types;
default_type  application/octet-stream;
#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
#                  '$status $body_bytes_sent "$http_referer" '
#                  '"$http_user_agent" "$http_x_forwarded_for"';
#access_log  logs/access.log  main;
sendfile        on;
#tcp_nopush     on;
charset utf-8;
#keepalive_timeout  0;
keepalive_timeout  120;
#gzip  on;
#填写自己的服务器ip和Tomcat端口
upstream local_tomcat {
server xxx.xxx.xxx.xxx:8080;
}
server {
listen 80 default_server;
listen 443 ssl;
charset utf-8;
#填写自己的网站域名
server_name  www.xxxx.xxx;
#证书文件
ssl_certificate C:/nginx-1.12.2/conf/1.crt;
#私钥文件
ssl_certificate_key C:/nginx-1.12.2/conf/2.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;
#配置HTTPS
location ~ /^[H,h][T,t][T,t][P,p][S,s]/ {
#网站根目录,为Tomcat下的wepapps目录
root C:/Tomcat7/apache-tomcat-7.0.82/webapps;
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $http_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ \.jsp$ {
root C:/Tomcat7/apache-tomcat-7.0.82/webapps;
proxy_pass http://local_tomcat;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-PORT $remote_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ \.html$ {
root C:/Tomcat7/apache-tomcat-7.0.82/webapps;
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $http_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location / {
root C:/Tomcat7/apache-tomcat-7.0.82/webapps;
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $http_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
#    proxy_pass   http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
#    root           html;
#    fastcgi_pass   127.0.0.1:9000;
#    fastcgi_index  index.php;
#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
#    include        fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
#    deny  all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#    listen       8000;
#    listen       somename:8080;
#    server_name  somename  alias  another.alias;
#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}
# HTTPS server
#
#server {
#    listen       443 ssl;
#    server_name  localhost;
#    ssl_certificate      cert.pem;
#    ssl_certificate_key  cert.key;
#    ssl_session_cache    shared:SSL:1m;
#    ssl_session_timeout  5m;
#    ssl_ciphers  HIGH:!aNULL:!MD5;
#    ssl_prefer_server_ciphers  on;
#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}
}

2 运行Nginx:

打开命令提示符跳转到nginx解压目录输入nginx

Nginx在Windows下的安装与使用过程详解

出现上述提示说明启动nginx失败,是由于电脑的80端口已经被占用,占用80端口的原因和解除方式都有很多种,例如SQLServer的ReportingServicesService占用,Apache,IIS,或者其他原因,笔者在这就不说明怎么解除占用了

解除占用后正常启动如下图:可以在任务管理器看到有两个nginx程序在运行,至于为什么是两个,可以查看Nginx官方的文档,不解释了

Nginx在Windows下的安装与使用过程详解

使用

开启Nginx,Tomcat。打开浏览器输入http(s)://你的域名/项目文件名/文件名即可进行访问

例如笔者配置的服务器(如果我的服务器开着的话可以访问。。。):

HTTPS:https://www.yunlingfly.cn/HelloNginx/index.html

Nginx在Windows下的安装与使用过程详解

HTTP:http://www.yunlingfly.cn/HelloNginx/index.jsp

Nginx在Windows下的安装与使用过程详解

(注意用https时看到浏览器上的安全两个绿字没有,可以用https装13了,https最多的还是用于表单提交,一般情况还是不要用,增加服务器压力,但是貌似ios应用貌似要求必须用https了,这里配置好https,就可以用来写带有https的servlet了)

使用注意事项:

1 首次安装Nginx时,不要直接点击nginx.exe程序,否则会导致很多问题,当配置完成后,以后再开启nginx即可直接点击nginx.exe程序,不需要再使用命令提示符操作,附nginx的基本操作指令:

开启:start nginx

检查配置文件:nginx -t -c C:/nginx-1.12.2/conf/nginx.conf
重新加载配置文件(很实用的指令):nginx -s reload
快速停止:nginx -s stop

完整停止:nginx -s quit

2 检测配置文件没有问题,但是使用HTTPS不能访问,可能是由于防火墙的原因,可以将其关闭试试,成功后,可以自己配置防火墙入网规则,将80(Nginx),443(SSL),1433(SQL Server),8080(Tomcat)等等端口添加至防火墙里,来继续开启防火墙(我当时就是在这麻烦了很久)

3 有些nginx.conf配置不正确会导致访问网页时样式文件(js、css)不能一起返回,经过测试,笔者的配置是没有这个问题的

到此这篇关于Nginx在Windows下的安装与使用的文章就介绍到这了,更多相关Windows安装使用Nginx内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_38187317/article/details/79334492

延伸 · 阅读

精彩推荐
  • NginxNginx实现跨域使用字体文件的配置详解

    Nginx实现跨域使用字体文件的配置详解

    这篇文章主要给大家介绍了关于Nginx实现跨域使用字体文件的配置方法,文中通过示例代码介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友...

    Carey6772019-12-01
  • NginxNginx配置指令location匹配符优先级和安全问题

    Nginx配置指令location匹配符优先级和安全问题

    使用nginx 很久了,它的性能高,稳定性表现也很好,得到了很多人的认可。特别是它的配置,有点像写程序一样,每行命令结尾一个";"号,语句块用"{}"括起...

    Nginx技术网4652019-10-16
  • NginxNginx最大连接数配置详解

    Nginx最大连接数配置详解

    这篇文章主要为大家详细介绍了Nginx最大连接数配置的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    shen1986234162022-07-26
  • Nginx详解基于CentOS 7配置Nginx自启动

    详解基于CentOS 7配置Nginx自启动

    这篇文章主要介绍了详解基于CentOS 7配置Nginx自启动,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧 ...

    Leshami5272019-12-16
  • Nginx分享最新版 nginx内置变量 大全

    分享最新版 nginx内置变量 大全

    在配置基于nginx服务器的网站时,必然会用到 nginx内置变量 ,下面笔者将它整理成列表,把最新版本的变量列出来,以方便做配置时查询 ...

    mdxy-dxy6392019-11-11
  • Nginx在Linux和Windows系统上安装Nginx服务器的教程

    在Linux和Windows系统上安装Nginx服务器的教程

    这篇文章主要介绍了在Linux和Windows系统上安装Nginx服务器的教程,Linux系统这里以CentOS为代表,需要的朋友可以参考下 ...

    goldensun3252019-10-31
  • Nginx一篇文章读懂nginx的gzip_static模块

    一篇文章读懂nginx的gzip_static模块

    gzip是针对于请求实时进行压缩,cpu开销大,gzip_static 完全可以在编译后使用压缩工具搞出来,下面这篇文章主要给大家介绍了如何通过一篇文章读懂nginx的gzi...

    乘风破浪202110552022-08-08
  • Nginxnginx优化的六点方法

    nginx优化的六点方法

    这篇文章主要介绍了nginx优化的六点方法,有对nginx优化不太熟悉的同学可以参考下...

    PHP开发社区4372021-03-08