nginx反向代理的nginx.conf配置

下面的配置是nginx.conf的示例
nginx反向代理 就是说把跨域的url通过本地代理的方式,变成同域的请求,如此来解决跨域问题
该配置下 通过http://localhost/html5/路径下的文件去请求http://localhost/request/的相关接口就相当于去请求http://localhost:8888/login/相关的接口

#user root owner;   #在mac中获取权限时需要将注释去掉
worker_processes  2; #启动进程,通常设置成和cpu的数量相等
events {
    worker_connections  1024; #单个后台worker process进程的最大并发链接数
}
http {
    include       mime.types; #设定mime类型,类型由mime.type文件定义
    default_type  application/octet-stream;
    #sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy)来输出文件,对于普通应用,
    #必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime.
    sendfile on;
    keepalive_timeout  65; #连接超时时间
    server {
        listen 80; #侦听80端口
        server_name localhost; # 定义使用www.xx.com访问
        charset utf-8;
        location / {
            root   html;
            add_header Cache-Control no-store;
            add_header 'Access-Control-Allow-Origin' '*';
            index  index.html index.htm;
        }
        location /request/ {
            root /;
            proxy_set_header  Host $host; #请求主机头字段,否则为服务器名称。
            proxy_headers_hash_max_size 1024; #存放http报文头的哈希表容量上限,默认为512个字符
            proxy_headers_hash_bucket_size 128; #设置头部哈希表大小 默认为64
            proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for ;
            proxy_set_header Accept-Encoding "";
            proxy_pass http://localhost:8888/login/; #请求替换地址 例如要请求http://localhost:8888/login/ 也就是请求http://localhost/request/
        }
        location ~ ^/html5/ {
            rewrite  /html5(.*)$ $1 break; #该指令根据表达式来重定向URI, 路径中存在/html5/的
            expires -1; #缓存
            root D:\html5; #前端静态文件物理路径 通过http://localhost/html5/来访问D盘html5下的文件夹
        }
    }
}
posted @ 2017-04-10 14:44  yanyuji  阅读(18557)  评论(0编辑  收藏  举报