通过nginx解决跨域访问,系统高可用等问题的配置文件示例。
worker_processes 1;
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;
keepalive_timeout 65;
gzip on;
# 通过upstream实现服务的高可用。
upstream ups_warnings {
server http://192.168.122.24:8083;
server http://192.168.122.25:8083 backup;
}
# 第一个server,监听80端口。
server {
# resolver dns.resolver;
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location ^~ /tags {
proxy_pass http://192.168.122.23:80/tags;
proxy_set_header X-Real-IP $remote_addr;
}
location ^~ /warnings/ {
rewrite /warnings/(.+)$ /$1 break;
proxy_pass http://ups_warnings;
proxy_set_header X-Real-IP $remote_addr;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
# 第二个server,监听81端口。
server {
# resolver dns.resolver;
listen 81;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location ^~ /tags {
proxy_pass http://192.168.122.23:80/tags;
proxy_set_header X-Real-IP $remote_addr;
}
location ^~ /warnings/ {
rewrite /warnings/(.+)$ /$1 break;
proxy_pass http://ups_warnings;
proxy_set_header X-Real-IP $remote_addr;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
注:当header中包含“_”的时候,nginx会致使header丢失。可以通过如下配置让nginx不过滤,但这种方式是不安全,且不推荐的。
ignore_invalid_headers on;
underscores_in_headers on;