Nginx在windows环境下的安装与简单配置

简介:

版权声明:本文为博主原创文章,未经博主允许不得转载。

一. 下载并安装Nginx

Nginx官网下载

我这里选取nginx/Windows-1.10.3版本,下载后解压出来即可,解压出来的路径不能含有中文

我解压后将其放置的路径如下

 

 二、开始运行

在当前目录下按住shift+鼠标右键,选择“在此处打开命令窗口”,然后输入start nginx

此时,就可以进入浏览器输入访问地址,http://127.0.0.1/或者http://localhost/即可访问

三、配置文件讲解

核心配置文件就是nginx.conf,该文件位于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
#user  nobody;
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;
 
     #keepalive_timeout  0;
     keepalive_timeout  65;
 
     #gzip  on;
 
     server {
         listen       80;
         server_name  localhost;
 
         #charset koi8-r;
 
         #access_log  logs/host.access.log  main;
 
         location / {
             root   html;
             index  index.html index.htm;
         }
 
         #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;
     #    }
     #}
 
}

  

 其中#代表注释

nginx我们最主要的作用是拿来做反向代理和负载均衡,这个我后面会着重讲解。同时它还是一个web服务器,与我们常用的Apache、tomcat、IIS一样,也可以用来托管web服务。

本章先暂时介绍下该配置文件中的几个重要参数,后面会对nginx部署php和Python项目再进行着重讲解,至于java的项目通常是tomcat+nginx同时进行配置,nginx用来做负载均衡和处理静态页。

1、定义Nginx运行的用户和用户组

1
#user  nobody;

2、nginx进程数,建议设置为等于CPU总核心数

1
worker_processes 1;

3、全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]

1
2
#error_log logs/error.log notice;
#error_log logs/error.log info;

4、进程文件

1
#pid        logs/nginx.pid;

5、工作模式与连接数上限:worker_connections是单个后台worker process进程的最大并发链接数,并发总数是 worker_processes 和 worker_connections 的乘积, 即 max_clients = worker_processes * worker_connections

1
2
3
events {
worker_connections 1024;
}

6、http下的一些配置及其意义

1
2
3
4
5
6
7
8
include mime.types;  #文件扩展名与文件类型映射表
default_type application /octet-stream #默认文件类型
sendfile on;  #开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来 输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置 为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常 把这个改成off。
autoindex on;  #开启目录列表访问,合适下载服务器,默认关闭。
tcp_nopush on;  #防止网络阻塞
tcp_nodelay on;  #防止网络阻塞
keepalive_timeout 120;  #长连接超时时间,单位是秒
gzip  on;  #开启gzip压缩输出

7、server虚拟主机的相关配置

我们平时配置各类服务器,配置最多的就是这些地方了

比如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
http{
  #虚拟主机1
  server{
   listen       80;    #监听端口,基于IP配置的时候变更此处,比如192.168.1.100:8080;
   server_name  www.xdw.com;   #主机域名,实际项目发布的话,填公网上的域名,本地部署的话,可以在C:\Windows\System32\drivers\etc\hosts文件中添加IP和域名的映射
   location / {       #映射解析,/代表根路径,此处解析还有正则表达式的解析方式,具体请参考http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_core_module.html#location
      root   E: /xdw/0221 ;    #工程所在路径
      index  index.html index.htm;   #首页(默认页)
   }
  }
 
  #虚拟主机2,可以同时配置多个虚拟主机
  server{
   listen       8080;
   server_name  localhost;
   location / {
      root   D: /xiangmu/txym_web ;
      index  index.html index.htm;
   }
  }
}

  看到这个虚拟主机的配置,相信配置过tomcat或者Apache的人都很熟悉的感觉。此篇就到此结束,下面还会更新linux下的配置,php和python项目的部署,反向代理和负载均衡,配合tomcat部署java项目。

目录
相关文章
|
15天前
|
iOS开发 MacOS Windows
Mac air使用Boot Camp安装win10 ,拷贝 Windows 文件时出错
Mac air使用Boot Camp安装win10 ,拷贝 Windows 文件时出错
|
14天前
|
缓存 前端开发 JavaScript
终极 Nginx 配置指南(全网最详细)
本文详细介绍了Nginx配置文件`nginx.conf`的基本结构及其优化方法。首先通过删除注释简化了原始配置,使其更易理解。接着,文章将`nginx.conf`分为全局块、events块和http块三部分进行详细解析,帮助读者更好地掌握其功能与配置。此外,还介绍了如何通过简单修改实现网站上线,并提供了Nginx的优化技巧,包括解决前端History模式下的404问题、配置反向代理、开启gzip压缩、设置维护页面、在同一IP上部署多个网站以及实现动静分离等。最后,附上了Nginx的基础命令,如安装、启动、重启和关闭等操作,方便读者实践应用。
204 84
终极 Nginx 配置指南(全网最详细)
|
2天前
|
JavaScript 应用服务中间件 开发工具
vue尚品汇商城项目-day07【53.nginx反向代理配置】
vue尚品汇商城项目-day07【53.nginx反向代理配置】
13 4
|
2天前
|
缓存 应用服务中间件 nginx
nginx如何配置?配置项都是什么意思?
nginx如何配置?配置项都是什么意思?
12 1
|
6天前
|
应用服务中间件 nginx Docker
docker应用部署---nginx部署的配置
这篇文章介绍了如何使用Docker部署Nginx服务器,包括搜索和拉取Nginx镜像、创建容器并设置端口映射和目录映射,以及如何创建一个测试页面并使用外部机器访问Nginx服务器。
|
5天前
|
存储 JavaScript 前端开发
Node 版本控制工具 NVM 的安装和使用(Windows)
本文介绍了NVM(Node Version Manager)的Windows版本——NVM for Windows的安装和使用方法,包括如何安装Node.js的特定版本、列出已安装版本、切换使用不同版本的Node.js,以及其他常用命令,以实现在Windows系统上对Node.js版本的便捷管理。
Node 版本控制工具 NVM 的安装和使用(Windows)
9-14|npm install --global windows-build-tools 安装太慢了,能够指定国内源
9-14|npm install --global windows-build-tools 安装太慢了,能够指定国内源
|
21天前
|
SQL JavaScript 数据库
sqlite在Windows环境下安装、使用、node.js连接
sqlite在Windows环境下安装、使用、node.js连接
|
26天前
|
应用服务中间件 nginx
一文搞定Nginx配置RTMP!
一文搞定Nginx配置RTMP!
60 3
|
27天前
|
Ubuntu 应用服务中间件 数据库
Nginx配置:阻止非国内IP地址访问的设置方法
此外,出于用户隐私和法律合规性的考虑,应慎重考虑阻止特定国家或地区IP地址的决策。在某些情况下,这可能被视为歧视性或违反当地法律。
49 2
下一篇
无影云桌面