1.首先去管网去下载nginx
下载地址:http://nginx.org/en/download.html
解压并打开截图如下(放的位置不要有中文要不然会启动出错):
2.启动一个tomcat服务器,端口为8080
启动成功后并访问:http://localhost:80880/tets/index.html
3.配置本地hosts
配置好之后,访问www.123.com:8080/test/index.html
此时得访问还需加上端口。
4.配置nginx.conf
配置反向代理
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; #监听得端口80
server_name www.123.com; #系统服务名称,即hosts所配置得名称
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://127.0.0.1:8080; #配置反向代理得地址
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;
#}
}
5.启动nginx,点击nginx.exe,并查看后台进程是否启动成功。
如果启动失败请查看log日志查找原因。
访问地址http://www.123.com/test/index.html,是没有端口得。
6.上面得nginx反向代理已配置成功,如果出现域名一样,端口不一样 则需要配置负载均衡,nginx.conf配置如下:
#负载均衡得地址
upstream server {
server 127.0.0.1:8080;
server 127.0.0.1:8081;
server 127.0.0.1:8082;
}
server {
listen 80; #监听得端口
server_name www.123.com;#系统服务名称,hosts配置得名称
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://server; #反向代理
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;
#}
}
7.重新启动nginx,输入命令:nginx.exe -s reload
访问地址http://www.123.com/test/index.html
轮询法
从上图可以看出每次访问得内容都不一样,8080/8081/8082轮询得去访问,nginx 默认得负载均衡算法是轮询法,你一次我一次。
加权轮询法
负载均衡地址后面的 weight=1,weight=2 是表示权重的意思,数字越大,权重越高。