Window下下载并配置Nginx以及SwitchHosts

一、介绍Nginx

  • 什么是Nginx

    • Nginx是一款高性能的http 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。由俄罗斯的程序设计师Igor Sysoev所开发,官方测试nginx能够支支撑5万并发链接,并且cpu、内存等资源消耗却非常低,运行非常稳定。

  • 应用场景

    • http服务器:Nginx是一个http服务可以独立提供http服务。可以做网页静态服务器(访问图片、js、css以及静态页面)。
    • 虚拟主机:可以实现在一台服务器虚拟出多个网站。例如个人网站使用的虚拟主机。
    • 反向代理,负载均衡(并发高):当网站的访问量达到一定程度后,单台服务器不能满足用户的请求时,需要用多台服务器集群可以使用nginx做反向代理。并且多台服务器可以平均分担负载,不会因为某台服务器负载高宕机而某台服务器闲置的情况。

二、下载Nginx

三、配置文件

  • 进入解压好的nginx-1.12.2文件,进入conf并打开nginx.conf文件进行配置
  • 注意下面代码中的一个service就是一个虚拟主机

  • 
    #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 / {
                #相对目录相对于安装目录,也可以是绝对目录:D:\nginx\nginx-1.12.2\html
                root   html;
                #欢迎页
                index  index.html index.htm;
            }
        }
    
        #可以配置其他的Service
        #server {
        #    监听端口
        #    listen       81;
        #    域名
        #    server_name  localhost;
        #
        #    charset koi8-r;
        #
        #    access_log  logs/host.access.log  main;
        #
        #    定位
        #    location / {
        #       相对目录相对于安装目录,也可以是绝对目录:D:\nginx\nginx-1.12.2\html81
        #       这里需要复制一份html并改名为html81
        #       root   html81;
        #       欢迎页
        #       index  index.html index.htm;
        #   }
        #}
    }
    

四、启动

  • 打开cmd,检查配置文件有没语法错误(注意路径以及命令:nginx.ext -t)
  • 看到了successful,则表示成功
  • 之后双击nginx.exe启动
  • 查看任务管理器
  • 之后输入网址:http://localhost/
  • 注意:因为nginx默认的端口是80,所以这里不需要写,如果换成别的端口就需要加上端口,如:localhost:81
  • 出现下图显示,则表示启动成功
  • 这样Nginx就配置好了

五、下载SwitchHosts

  • 作用:
    • SwitchHosts是一个Hosts快速切换小工具,作用是用来一键切换Hosts配置文件,非常实用。
  • 下载地址:https://github.com/oldj/SwitchHosts/releases
    • 注意:进入软件后,点击文件 ->设置 ->可以选择中文

六、新建本地hosts

  • 点击文件 ->新建 ->输入host方案名
  • 这里需要将按钮滑动编程绿色的,在右边空白处输入网址以及域名
  • 我这里网址是本机的IP,默认端口为80(这里是Nginx)
  • 这样就测试成功了

猜你喜欢

转载自blog.csdn.net/Future_LL/article/details/84112281