1. 安装
sudo apt-get install nginx
2. 配置nginx
sudo gedit /etc/nginx/nginx.conf
user www-data; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/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 /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; #real server 即66和67 upstream cluster { server localhost:8080; server 192.168.2.67:8080; } server { listen 80; server_name 192.168.2.68; location ~ ^/NginxStatus/ { stub_status on; access_log off; } location ~ ^/(WEB-INF)/ { deny all; } #将所有访问happyAdmin的请求都转发到67 location /happyAdmin/ { proxy_pass http://192.168.2.67:8080/happyAdmin/; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } #将剩余请求按照cluster配置进行负载 location / { proxy_pass http://cluster; proxy_set_header Host $host; } #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; } } }
3. 重启
sudo service nginx restart
如果出现错误可以通过/var/log/nginx/error.log /var/log/nginx/access.log进行调试。