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

it2022-05-24  82

每天学习一点点 编程PDF电子书、视频教程免费下载: http://www.shitanlife.com/code

 

 

一. 下载并安装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项目。

转载于:https://www.cnblogs.com/scode2/p/8718523.html


最新回复(0)