官网下载 nginx-1.8.1.tar ,编辑\nginx-1.8.1\conf\nginx.conf文件
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;
######################################### upstream 后面是真是服务器的列表的名字 ,花括号里面是主机名和端口 做集群一定要配置
upstream tomcat_server {# 注意这里
#ip_hash 启用表示,之后这个ip将只访问第一次访问的tomcat,session就会是同一个
ip_hash;
#localhost因为是我本地部署,所以不同的服务器,修改成ip即可 weight越高访问到的几率越高
server localhost:8082 weight=1;
server localhost:8084 weight=1;
}
#########################################
server {
listen 80; # 端口号不能是80,否则不能跳转到真实的服务器列表其中的一台,可能是被占用之类的
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root html;
#index index.html index.htm;
#########################################
proxy_pass http://tomcat_server; # proxy_pass 后面是 http://加真实的服务器列表的名字,表示用这个server的时候,跳到列表里的一台服务器
#proxy_redirect off;
#这个必须设置 否则访问有问题
proxy_set_header Host $host:$server_port; # 如果不配这个 ,js和css 文件无法访问
#客户端地址
proxy_set_header X-Real-IP $remote_addr;
#客户端端口
proxy_set_header X-Real-PORT $remote_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#加快切换速度
proxy_connect_timeout 1;
proxy_read_timeout 1;
proxy_send_timeout 1;
#########################################
}
#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;
# }
#}
}