首页 > 代码库 > nginx 负载均衡配置

nginx 负载均衡配置

首先解决wampserver不能使用本机ip地址访问的问题:

进入wamp目录下的alias 目录, 打开phpmyadmin.conf  文件

找到如下代码

 

Options FollowSymLinks
AllowOverride None
Order deny,allow
#Deny from all
Allow from all

 

把Deny from all改成Allow from all就行了。
 
下载nginx
 
将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;

upstream backend {

#ip_hash;
server 172.31.251.237:5000;
server 172.31.251.237:8080;

}


#gzip on;

server {
listen 80 default_server;
server_name www.hello.com;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
#设置主机头和客户端真实地址,以便服务器获取客户端真实IP

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

 

#禁用缓存

proxy_buffering off;

 

#反向代理的地址

proxy_pass http://backend;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.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;
# }
#}

}

 

172.31.251.237为我的电脑在公网的ip地址

5000端口是我用flask搭建的web应用所在的端口

8080端口是zen cart所用端口

 

nginx -s reload 重启nginx

start nginx 启动nginx

nginx -s stop  停止nginx

 

启动nginx,访问127.0.0.1

即可通过负载均衡实现对两个应用的轮流访问

nginx 负载均衡配置