nginx.conf
文件
user nginx;
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;
upstream kedamall{
# 我们只配置了一台网关,所以只有一个server
server 172.27.0.12:8081;
}
include /etc/nginx/conf.d/*.conf;
}
kedamall.conf
文件
server {
listen 80;
# server_name是按照Host进行匹配
server_name kedamall.com *.kedamall.com;
location /static/ {
# root:根文件夹下查找
root /usr/share/nginx/html;
}
location / {
# proxy_pass含义:将/代理给谁
# 代理给上游服务器(upstream)的名字
#nginx在代理给网关的时候,默认会丢掉例如Host等的信息
#因此需要如下设置:
proxy_pass http://kedamall;
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_set_header REMOTE-HOST $remote_addr;
}
#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 /usr/share/nginx/html;
}
}
使用 Nginx 负载平衡的配置如下(使用upstream):
http{
upstream myapp1 {
# 一个server代表一个服务器
server srv1.example.com;
server srv2.example.com;
server srv3.example.com;
}
server {
listen 80;
location /{
proxy_pass http://myapp1;
}
}
}