Nginx原理介绍以及教你Windows系统下快速上手(附常见问题解决方法)

前言

在互联网项目中,当访问量达到一定程度的时候,单台服务器并不能够满足用户的请求,需要多台服务器,这时候就可以使用Nginx做反向代理,并且多台服务器可以平均分担负载,尽量的避免出现某台服务器负载高而某台服务器闲置的情况出现,充分利用资源,降低企业成本。

什么是Nginx

Nginx是一个http服务器,可以独立提供http服务。可以做网页静态服务器。

虚拟主机,可以实现在一台服务器虚拟出多个网站

反向代理,在客户端与服务器端之间建立一个关卡

负载均衡,使资源利用最大化

怎么在Windows下使用Nginx呢?

第一步、下载一个Nginx,网址我都准备好了,各位看官自便http://nginx.org/

第二步、直接解压安装,安装完之后会变成这个样子

第三步、先不着急改配置,我们先运行试试,

点击上图exe文件,即可实现运行哦,闪退表示运行成功,也可自己打开资源管理器看看是否运行成功(如果不放心的话)

如下图出现两个标识就是运行成功了

第四步,修改配置文件,配置文件中每个配置的详解我就直接贴出来了(conf文件夹下的ngnix.cof文件)

PS:大家先看下配置文件的具体含义,具体修改在下面


#user  nobody;

# 指定nginx进程数
worker_processes  1;

# 全局错误日志及PID文件
#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服务器,利用它的反向代理功能提供负载均衡支持
http {

    #设定mime类型,类型由mime.type文件定义
    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 指令指定 nginx是否调用sendfile 函数(zero copy 方式)来输出文件,对于普通应用
    sendfile        on;
    #tcp_nopush     on;

    
    # 连接超时时间
    #keepalive_timeout  0;
    keepalive_timeout  65;

    #开启gzip压缩,压缩html
    #gzip  on;

    
###################################
    # 设定负载均衡的服务器列表 支持多组的负载均衡,可以配置多个upstream 来服务于不同的Server.
    # nginx 的 upstream 支持几种方式的分配
    # 1.轮询(默认)每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
    # 2.weight 指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
    # 3.ip_hash 每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题
    # 4.fair
    # 5.url_hash #Urlhash
    upstream tomcat_server{
        #weigth参数表示权值,权值越高被分配到的几率越大
        #1.down 表示当前的server暂时不参与负载
        #2.weight 默认为1 weight越大,负载的权重就越大
        #3.backup 其他所有的非backup机器down或者忙的时候,请求backup机器,所以这台机器的压力最轻,备用机器  
        server localhost:8080    weight=55;
        server localhost:8081    weight=44;
        server localhost:8082    weight=1;
        server 192.0.0.1    weight=1;
    }

    # 配置代理服务器的地址,即Nginx安装的服务器地址、监听端口、默认地址
    server {
    
        #1.监听80端口
        listen       80;
        
        #对于server_name,如果需要将多个域名的请求进行反向代理,可以配置多个server_name来满足要求
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
        
            # 默认主页目录在nginx安装目录的html子目录
            root   html;
            index  index.html index.htm;
            proxy_pass http://tomcat_server;
        }
        location ~\.(html|js|css|png|gif)${
                root     F:\LoveToTravel\apache-tomcat-1\apache-tomcat-7.0.85\webapps\ROOT
        }
         #访问本地E:/source文件夹 访问路径为localhost:8099/file/a.png 实际访问路径为 E:/source/file/a.png
       # location /file/ {
        #    root E:/source/;
        #   autoindex on;
       # }


        #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;
    #    }
    #}

}
该如何去修改呢?(以配置tomcat集群为实例)

自己在upstream 后定义一个集群,策略按自己需求自定义

配置Ngnix默认打开路径(笔者的是Ngnix目录下的html目录下)

指定代理的集群

第五步、在浏览器输入localhost:8080,如果能访问到你指定的目录,表示运行成功

第六步、常见BUG详解

一、配置修改未生效:重启Ngnix(初学者可直接进任务管理器结束进程)

二、端口被占用,Windows系统下具有一个默认的浏览器占用着80端口,将其修改就OK

转账请注明出处,掌声送给社会人

猜你喜欢

转载自blog.csdn.net/SCDN_CP/article/details/84226477