nginx配置二级域名访问集群tomcat和nginx windows版安装 启动

1个nginx和2个tomcat,配置D:\tools\nginx\nginx-1.9.3\conf\nginx.conf文件,实现访问http://localhost/bb/test.html,http://localhost/aa/test.html

如果想模拟某个网站如qq.com,只需配置windows hosts文件 

win7下C:\Windows\System32\drivers\etc\hosts

127.0.0.1 qq.com

就可以实现如下效果

http://www.qq.com/bb/test.html

nginx.conf 内容如下

#为单行注释符号

#配置用户和用户组,可以不设
#user www-data;

#工作进程数,建议设置为CPU的总核数
worker_processes  4;

#全局错误日志定义类型,日志等级从低到高依次为: debug | info | notice | warn | error | crit
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
error_log  logs/error.log  info;

#记录主进程ID的文件
pid        logs/nginx.pid;

#一个进程能打开的文件描述符最大值,理论上该值因该是最多能打开的文件数除以进程数。但是由于nginx负载并不是完全均衡的,
#所以这个值最好等于最多能打开的文件数。执行 sysctl -a | grep fs.file 可以看到linux文件描述符。
worker_rlimit_nofile 65535;

events {
#单个进程允许的最大连接数
    worker_connections  1024;
}

#设定http服务器,利用它的反向代理功能提供负载均衡支持
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 记录了哪些用户,哪些页面以及用户浏览器、ip和其他的访问信息
    access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
        #开启gzip压缩
    gzip  on;
        gzip_disable "MSIE [1-6]\.(?!.*SV1)";
#upstream作负载均衡,在此配置需要轮询的服务器地址和端口号,max_fails为允许请求失败的次数,默认为1.
#weight为轮询权重,根据不同的权重分配可以用来平衡服务器的访问率。
   upstream mysvr {
                #8080 tomcat监听端口
        server 127.0.0.1:8080 max_fails=1 weight=1;

    }
    upstream mysvr1 {
                #9080 tomcat监听端口
        server 127.0.0.1:9080 max_fails=1 weight=1;

    }

#主机配置
    server {
            #监听端口
        listen       80;
        #域名
        server_name  localhost;

        #字符集
        charset utf-8;

        #单独的access_log文件
        access_log  logs/127.0.0.1.access.log  main;
                #反向代理配置,将所有请求为http://hostname的请求全部转发到upstream中定义的目标服务器中。
        location / {
        #此处配置的域名必须与upstream的域名一致,才能转发。
            proxy_pass     http://mysvr;
            proxy_set_header   X-Real-IP $remote_addr;

            root   html;
            index  index.html index.htm;
        }
        location /aa {#这个/aa是url中的
        #此处配置的域名必须与upstream的域名一致,才能转发。
            proxy_pass     http://mysvr1/aa;#这个/aa是tomcat中的server.xml的Context的path值/aa
            proxy_set_header   X-Real-IP $remote_addr;

            root   html;
            index  index.html index.htm;
        }
        location /bb {
        #此处配置的域名必须与upstream的域名一致,才能转发。
            proxy_pass     http://mysvr/bb;
            proxy_set_header   X-Real-IP $remote_addr;

            root   html;
            index  index.html index.htm;
        }
                #图片缓存时间设置
                location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
                         {
                         expires 10d;
                         }
                 #JS和CSS缓存时间设置
                location ~ .*\.(js|css)?$
                         {
                         expires 1h;
                         }
        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;
        }
                #启用nginx status 监听页面
        location /nginxstatus {

            stub_status on;

            access_log on;

        }

        # 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,如新浪、网易、腾讯等;新近发现 Nginx 技术在国内日趋火热,越来越多的网站开始应用部署Nginx。

一、首先去官网下载 nginx1.0.11的Windows版本,官网下载:http://nginx.org/download/nginx-1.0.11.zip

下载到软件包后,解压 nginx-nginx1.0.11.zip 包到你喜欢的根目录,并将目录名改为nginx。

然后,执行下列操作:

cd nginx

start nginx

这样,nginx 服务就启动了。打开任务管理器,查看 nginx.exe 进程,有二个进程会显示,占用系统资源,那是相当的少。然后再打开浏览器,输入 http://127.0.0.1/  就可以看到nginx的欢迎页面了,非常友好

 nginx -s stop          // 停止nginx
nginx -s reload       // 重新加载配置文件
nginx -s quit          // 退出nginx

 

 

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值