nginx 根据域名反向代理

本文详细解释了Nginx配置中的关键部分,包括工作进程、连接数设置、HTTP模块、upstream负载均衡、服务器监听和代理设置,以及CORS跨域访问控制。
worker_processes  1;
events {
    worker_connections  1024;
}
http{
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  165;
    log_format  main  'host=$host|$http_x_forwarded_for|$http_x_real_ip|remote_addr=$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';              
    upstream uuuuu{
        #远程服务器端口是80,就省略端口
        server xxxx.xxx.com(你的域名或IP):端口 max_fails=10 fail_timeout=300s weight=1;
        #server 127.0.0.1:8901 weight=1;
    }
    server {
        # nginx 对外暴漏的端口
        listen       80;
        server_name  xxxx.xxx.com(你的域名或IP);#服务接口在哪里,这个就写那个接口所在的服务器域名或IP
        access_log  logs/host.access.log  main;
        location / {
            proxy_pass http://uuuuu/;
            default_type application/json;
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
            add_header 'Access-Control-Max-Age' 1728000;
            if ($request_method = 'OPTIONS') {
               return 204;
            }
            client_max_body_size  500m;
            proxy_http_version 1.1;
            proxy_connect_timeout 980;
            proxy_read_timeout 800;
            proxy_send_timeout 800;
            proxy_redirect      off;
            proxy_set_header    Host             $server_name;# 远程是域名的时候最好使用server_name
            proxy_set_header    X-Real-IP        $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            add_header Content-Type 'application/json; charset=utf-8';
        }
    }
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值