Nginx 使用

Nginx 是一款由俄罗斯人开发的 轻量级 高效率 反向代理的服务器,既可以代理邮箱服务器(IMAP、pop3、SMTP),也可以代理web服务器

为了防止单个服务器崩坏,进行服务器集群的负载均衡

拥有较好的跨平台性

服务器集群问题:无法共享session 解决方案 redis

 正向代理 :一般情况下 在客户端  代理客户端 的请求到服务器端

反向代理:在服务器端 是服务器的代理  接受客户端的请求 然后决定分配给那个服务器

 

 Nginx是多进程形式工作的  一个主进程和多个工作进程 

一、master进程  主进程 控制worker进程  工作进程

二、worker进程   如果worker进程异常退出  会自动重启 worker进程, 在网络请求到时,多个worker进程之间是平等竞争的,各个进程之间是互相独立的。一个请求只能交给一个进程处理;同一时间内,一个进程也只能处理一个请求。

worker进程数可以设置 一般设置与处理核心数相同  原因:nginx的进程模型 和事件处理模型不可分开的。(Nginx-1.10.3\conf\nginx.conf中设置)

 


使用: 

tomcat-1  端口配置   这是tomcat8.x版本

<!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/>


    <!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
    -->
    <Connector URIEncoding="utf-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>


tomcat-2 端口设置 tomcat7.x版本

<!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL HTTP/1.1 Connector on port 8080
    -->
    <Connector port="8081" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8444" />

    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8010" protocol="AJP/1.3" redirectPort="8444" />

 

配置好tomcat后  去百度下载Nginx的安装文件  或者解压版

 

打开 conf/ nginx.conf中进行以下配置

#user  nobody;设置worker进程数
worker_processes  4;

#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;
	#服务器集群配置
    upstream jeffrey.com{#服务器集群名称
	server	127.0.0.1:8080 weight=2; #服务器配置,weight是权重,权重越大,分配的概率越大。前面配置设置好的tomcat地址和端口
	server	127.0.0.1:8081 weight=1;
    }
    server {
        listen       80;#设置监听就是用户访问 接口80与浏览器端口相同 访问时不用写端口号
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        #location / {
        #    root   html;
        #   index  index.html index.htm;
        #}
		location / {
            proxy_pass   http://jeffrey.com ; #与服务器集群的名称一致
            proxy_redirect  default;
        }


        #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服务器

然后开启Nginx 

一、双击.exe文件     由于服务器端口已经被Nginx代理  又因为监听端口为80因此在浏览器 输入localhost即可访问不同浏览器

二、使用命令

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值