如何在nginx上配置简单的目录浏览与负载均衡策略
##1.configure autoindex on subdirectory
root /var/www/beyondlinux.com/htdocs/;
location ^~ /files/pub/weather/www.weather.com.cn{
autoindex on;
}
location / {
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_pass http://main;
}
##2.nginx load balance
prepare the nginx configuration file for slave instance.
Let us assume that we will start 3 nginx instance, listening on 80, 81, 82.
The web site is served through port 80, and the user request comes to 80 are redirected to 81, 82 for load balance; so that you can maintenance each instance without interruption of the whole server.
$cd /opt/nginx (change directory to where nginx is installed)
$cp conf/nginx.conf conf/nginx.81.conf
$cp conf/nginx.conf conf/nginx.82.conf
$mv conf/nginx.conf conf/nginx.backup.conf
And then change the listen port 80 to 81,82 respectively in nginx.81.conf and nginx.82.conf;
also don’t forget to comment out the pid line and change the pid file for each instance so that each instance can be stopped normally:
pid logs/nginx.81.pid; ### in file nginx.81.conf
pid logs/nginx.82.pid; ### in file nginx.82.conf
$vi conf/nginx.conf copy the following content to listen on port 80 whose requests are routed another two nginx port at 81 and 82.
###deploy multiple nginx instance for load balance on 1 server Let’s assume the server ip is 106.187.45.82.
upstream main {
server 106.187.45.82:81;
server 106.187.45.82:82;
}
server {
listen 106.187.45.82:80
location / {
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_pass http://main;
}
}
Restart the new server
$sbin/nginx -s stop
Then you can start the new instance by:
cd /opt/nginx
sbin/nginx -c conf/nginx.81.conf
sbin/nginx -c conf/nginx.82.conf
sbin/nginx -c conf/nginx.conf
You can reload configuration without full restart
sbin/nginx -s reload -c conf/nginx.conf