nginx-8080.conf:
#user nobody;
#工作线程数 一般与逻辑CUP个数一至
worker_processes 8;
error_log /home/yanlei/logs/nginx-8080.log;
#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;
#charset gbk;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$proxy_add_x_forwarded_for" ';
#上传文件大小
client_max_body_size 100m;
#请求后端代理超时时间
proxy_read_timeout 180;
#access_log logs/access.log main;
#输出访问日志
access_log logs/nginx-access-8080.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#获取请求实际IP地址,来源于X-Forwarded-For,排除掉set_real_ip_from定义的多个IP(一
般为上游负载IP)
#X-Forwarded-For= 211.123.12.65,192.168.1.100,192.168.1.102
#211.123.12.65为实际IP,后两个为上游负载,需要排除
#获取实际IP之后, $remote_addr=实际IP
#upstream 中配置的负载策略ip_hash,就可以按照实际IP进行hash,否则
#nginx取到的每个请求的IP都是上游负载IP,导至只请求后端一个服务。
#其它服务不请求,起不到负载均衡的作用。
set_real_ip_from 192.168.1.100;
set_real_ip_from 192.168.1.101;
set_real_ip_from 192.168.1.102;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
upstream MyWeb{
ip_hash;
server 192.168.1.1:8080;
server 192.168.1.2:8080;
}
server {
listen 8080;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
location /resource{
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://MyWeb;
#$proxy_add_x_forwarded_for 如果ningx之前有N个负载均衡,它的值为
#实际IP地址,第一个负载IP,第二个负载IP,
#后端应用可以从请求的HEADER 中获取请求实际IP: header_name=X-Forwarded-For
}
#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;
# }
#}
}
startup.sh
#!/bin/bash
/home/yanlei/nginx/sbin/nginx -p /home/yanlei/nginx -c /home/yanlei/conf/nginx-8080.conf
-g "pid /home/yanlei/pid/nginx-8080.pid;"
stop.sh
#!/bin/bash
/home/yanlei/nginx/sbin/nginx -p /home/yanlei/nginx -c /home/yanlei/conf/nginx-8080.conf
-g "pid /home/yanlei/pid/nginx-8080.pid;" -s stop
reload.sh
#!/bin/bash
/home/yanlei/nginx/sbin/nginx -p /home/yanlei/nginx -c /home/yanlei/conf/nginx-8080.conf
-g "pid /home/yanlei/pid/nginx-8080.pid;" -s stop
改完配置后,使用 reload.sh重新加载配置,如果配置有错误会得到提示,可继续修改,对生产无影响。
如果配置无错误,新的配置生效,生产服务无间断。