nginx

什么是nginx?
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好。
为什么使用nginx?

国内有哪些企业使用nginx
国内大多数企业都使用nginx!

京东:
淘宝:
新浪:

安装nginx
nginx使用c语言写。所以我们需要安装nginx的环境。
yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
下载nginx
https://nginx.org/en/download.html
下载的是源码:--编译----安装
把下载的nginx放入linux并解压
tar -zxvf XXX.tar.gz
创建一个安装目录
mkdir -p /usr/local/app/nginx
进入nginx解压的目录

进入nginx安装的目录
ngin的配置文件的介绍


#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 81;#该服务监听的端口号
        server_name  localhost;#监听的服务域名
          location /{
             root main;
             index main.html;
          }
    }

    
    #反向代理的一个服务
    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的核心功能
1 nginx反向代理功能
正向代理
代理的为客户端,对于服务器不知道真实客户的信息。例如:FQ软件。

反向代理服务器
代理的为服务器端。对于客户来说不知道服务器的信息。例如: nginx

项目部署图

启动web项目
配置nginx

    server {
       listen 82;
       server_name localhost;
       location /{
           # 代理的真实web服务器地址
          proxy_pass   http://192.168.111.132:8080;
       }
    }

启动nginx
./usr/nginx/sbin/nginx
2 负载均衡
负载均衡(Load Balance [4])其意思就是把请求分摊到多个操作单元上进行执行,例如Web服务器、FTP服务器、企业关键应用服务器和其它关键任务服务器等,从而共同完成工作任务。

准备两台
1台跑nginx
1台跑两个tomcat项目。8001 8002
配置nginx负载均衡

测试

posted on 2024-12-29 17:29  小木不痞  阅读(118)  评论(0)    收藏  举报

导航