设为首页 收藏本站
查看: 867|回复: 0

[经验分享] Nginx在windows环境下的安装与简单配置

[复制链接]

尚未签到

发表于 2018-6-17 16:33:59 | 显示全部楼层 |阅读模式
  版权声明:本文为博主原创文章,未经博主允许不得转载。
  一. 下载并安装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;
  }
  }
  }

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-525091-1-1.html 上篇帖子: Windows Server 2012 R2 路由功能部署 下篇帖子: Win2012R2 Hyper-V初级教程22 -- 虚拟机的导入与导出
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表