nginx代理转发配置

遇到前端跨域问题,不想改代码,解决方案有node.js或nginx,因工作中nginx接触较多,所以选择nginx来处理
顺便拿nginx做前端资源转发

主要内容

  • nginx配置转发
  • 日志配置

转发

nginx.conf

worker_processes  1;    #工作进程数

events {
    worker_connections  1024;    #单个工作进程可以允许同时建立外部连接的数量
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    #日志模版
    log_format  main
           '$remote_user [$time_local]  $http_x_Forwarded_for $remote_addr  $request '
           '$http_x_forwarded_for '
           '$upstream_addr '
           'ups_resp_time: $upstream_response_time '
           'request_time: $request_time';
    access_log  logs/access.log  main; #日志路径及模版配置

    sendfile        on;
    keepalive_timeout  65;

    include gzip.conf;     #导入gzip压缩配置
    include proxy.conf;   #导入http_proxy 设置
    include sites/nginxconf;    #导入转发配置

转发配置

server {
       listen       8571; #nginx端口设置
       server_name  blog.etiantian.org;
       #location / {
       #    proxy_pass http://blog_real_servers;
       #    #include proxy.conf;
       #}
       access_log  logs/access.log  main;

       #转发到指定域名
       location /baidu/ {  #匹配需要转发的url
            proxy_pass http://www.dxmbaoxian.com/;  #转发目的(结尾带/ 则不将/baidu/ 放到转发的url中)
            proxy_connect_timeout 5s;    #超时时间
            proxy_set_header Host "www.dxmbaoxian.com"; 
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header REMOTE-HOST $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            #access_log  logs/access-baidu.log  main; # 转发日志输出
}
  • proxy_pass 参数的相对路径与绝对路径可参考https://www.jianshu.com/p/b113bd14f584
  • nginx其他资料 https://www.jianshu.com/p/1fe5a05f28f4

你可能感兴趣的:(nginx代理转发配置)