nginx搭建(详细篇-windows)

 

下面只有window下的搭建,想看Linux的可以退出或者点击这儿,这里面也只有配置,没有错误介绍的,简单点说,遇到坑的话自己重新去搜吧

一.windows下搭建nginx服务器

1.官网下载http://nginx.org/en/download.html

windows使用的为 nginx-1.14.0.zip

2.先解压缩到任意位置,

解压缩后应该为如下文件

 

3.双击nginx.exe文件,打开任务管理器,应当看见如下内容,代表启动成功

 

 

 

4.在conf文件夹下有一个nginx.conf文件


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

}

 

在目录中建立一个Tomcat文件夹用于存放Tomcat服务器

复制多份Tomcat放入Tomcat文件夹下

修改Tomcat的conf目录下的server.xml文件 

<Server port="8006" shutdown="SHUTDOWN">

 <Connector URIEncoding="utf-8" connectionTimeout="20000" port="18081" protocol="HTTP/1.1" redirectPort="8443"/>

<Connector port="8010" protocol="AJP/1.3" redirectPort="8443"/>

保证两个server.xml目录下的这3个部分端口号不一致。

这时,我们放一个war包到Tomcat的webapps目录下,

然后点击bin目录下面的startup.bat,启动Tomcat

在浏览器输入网址http://localhost:18081/可以看见Tomcat画面,加上项目名后可以看见项目,这时候Tomcat启动成功了

我这儿有两个Tomcat,另一个网址为http://localhost:18082/

我们在去nignx的nginx.conf文件中修改配置

去除注释后如下

worker_processes  1;
nts {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;
        
        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}

listen为端口号

server_name 为别名

location 为地址。

简单点说当我输入localhost时会匹配location 前往index.html.

我这边要对需要其进行修改

在servery上加一部分,修改location 中内容,

upstream local_tomcat { 
        
        server localhost:18081 weight=1;  
        server localhost:18082 weight=1;  
    }
    server {
        listen       80;
        server_name  localhost;

        location / {
            proxy_pass http://local_tomcat; 
        }
    
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        
    }

意思是当我点击localhost会调用localhost:18081或者localhost:18082 其中weight代表权重,值越大,出现的可能性越大

这时候我们输入网址localhost+项目名即可登陆,它会让我们随机登陆一个服务器,但是这会出现一个问题,因为服务器是变动的,session存储我们的账号信息,不会随着变动,我们需要加上一个ip_hash;

这时我们在登陆服务器,就可以保证不会重新进入登录页面

upstream local_tomcat { 
        ip_hash;
        server localhost:18081 weight=1;  
        server localhost:18082 weight=1;  
    }

PS:本来还想写一个Linux下的详细篇的,但是超哥说不要用ip_hash;这个的实现是将ip生成一个hash值,下次访问的时候根据ip前往固定的Tomcat,现在需要的是有多个Tomcat,每个上面放一个项目,某些项目之间是可以互相跳转的,共用一个用户表。可能用户登陆的这个Tomcat服务器没有那一个模块。所以需要我使用缓存服务器。我去研究缓存服务器了Redis接触过,但没配置过,memcache第一次听。希望给个好运,别报错

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值