分类 nginx 下的文章

首先需求是这样的:需要根据用户输入的域名或IP(内网或外网)来加载不同的静态资源。

之前功能是好的,最近使用了iis做反向代理发现获取到的是服务器的IP,排查了好久才找到原因出在反向代理上:

  • 代理为了提高性能,一些Http头部信息不回转发给后台服务器,其中就包括代理服务器的host信息,而tomcat中对于request.getServerName()的实现,就是取这个host信息,如果http
    header总没设置,则取应用所在服务器IP地址。

所以,需要设置下让代理服务器把Host转发给后台服务器。网上类似的资料都是apache和nginx的解决方法,iis的资料很少,搜了好久才找到,这里总结下:

1、apache:在<VirtualHost/>标签中的最后添加 ProxyPreserveHost on

<VirtualHost *:80>
    RewriteEngine on
    ProxyPass /TLimages/ !
    ProxyPass /imagelist/ !
    ProxyPass /xiazai/ !
    ProxyPass /ad/ !
    ProxyPass / balancer://proxy/
    ProxyPassReverse / balancer://proxy/
    ProxyPreserveHost on
</VirtualHost>

2、nginx:在location {…}中添加 proxy_set_header Host $host

location ^~/proxy_path/ {
    root "/www/html";
    index index.html;
    proxy_pass http://192.168.223.137/;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

3、iis:设置preserveHostHeader:true

%windir%\system32\inetsrv\appcm d.exe set config -section:system.webServer/proxy -preserveHostHeader:true /commit:apphost

相关文章:http://www.yyjjssnn.cn/articles/842.html

调试用的

log_format json  '{'
        '"time_local":"$time_local",'
        '"time_iso8601":"$time_iso8601",'
        '"host":"$host",'
        '"remote_addr":"$remote_addr",'
        '"http_x_forwarded_for":"$http_x_forwarded_for",'
        # $remote_user等同于用户名,由ngx_http_auth_basic_module认证
        '"remote_user":"$remote_user",'
        # 客户端port
        '"remote_port":"$remote_port",'
        '"request":"$request",'
        # 含有参数的完整的初始URI
        '"request_uri":"$request_uri",'
        # $query_string与$args一样
        '"args":"$args",'
        '"query_string":"$query_string",'
        '"status":"$status",'
        # 协议,通常是“GET”或“POST”
        '"request_method":"$request_method",'
        '"server_protocol":"$server_protocol",'
        '"request_body":"$request_body",'
        # 当前请求的文件的路径名,由root或alias和URI request组合而成
        '"request_filename":"$request_filename",'
        '"body_bytes_sent":"$body_bytes_sent",'
        '"http_referer":"$http_referer",'
        '"http_user_agent":"$http_user_agent",'
        '"upstream_addr":"$upstream_addr",'
        '"upstream_response_time":"$upstream_response_time",'
        '"upstream_status":"$upstream_status",'
        '"request_time":"$request_time",'
        '"cookie_uid":"$cookie_uid"'
        '}';

loghao 记录下来以后用

    log_format  loghao  '[$time_local] - $remote_user $http_x_forwarded_for - $host$request_uri $status $request_method $server_protocol $body_bytes_sent '
        '"$http_referer" "$request_body" "$http_user_agent" "$request_filename" '
        '"$upstream_addr" "$upstream_response_time" "$upstream_status" $request_time';

相关文章:
http://www.hebinghua.com/linux/25.html

使用nginx做反向代理,当后端服务器需要认证时,需要把认证的http header也传到后端服务器上去,配置为:
proxy_set_header Authorization $http_authorization;
这样配置后,服务器会发出一个认证窗口出来,提示用户输入用户名密码。

如果不想让用户输入用户名密码,可用下面的配置:

# 隐藏发给用户的认证http header,相当于不提示用户输用户名密码了
proxy_hide_header WWW-Authenticate;
# 发送httpd 认证 header给后端服务器。
proxy_set_header Authorization "Basic dXNlcjpwYXNzd29yZA==";

dXNlcjpwYXNzd29yZA==是base64(user:pass)得到的。

解释一下上面两个http header:

WWW-Authenticate: 这是GET的时候带的,服务器发给客户端的。表明客户端请求实体应该使用的授权方案

示例:WWW-Authenticate: Basic

Basic是基本的http认证方式,除此之外还有NTLM等,NTLM是微软的,nginx目前不支持。

Authorization 这个是用户输入用户名和密码后,POST到服务器的时候带的。HTTP授权的授权证书
示例:Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==

下面是一个提示用户输入用户名和密码的httpd auth反向代理的配置例子:

upstream dwserver {          
      server 10.18.0.1:81 max_fails=3 fail_timeout=30s;
   }

   server {
        listen       80;
        server_name  blog.gamecf.cn;
        location ~ .*$ {
            proxy_redirect off;
#            proxy_pass_request_headers on;
            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_hide_header Authorization;
#            proxy_hide_header WWW-Authenticate;
            proxy_set_header Authorization $http_authorization;
#            proxy_no_cache $cookie_nocache  $arg_nocache$arg_comment;
#            proxy_no_cache $http_pragma     $http_authorization;
#            proxy_cache_bypass $cookie_nocache $arg_nocache $arg_comment;
#            proxy_cache_bypass $http_pragma $http_authorization;
            client_max_body_size 50m;
            client_body_buffer_size 256k;
            proxy_connect_timeout 60;
            proxy_send_timeout 60;
            proxy_read_timeout 120;
            proxy_buffer_size 24k;
            proxy_buffers 4 64k;
            proxy_busy_buffers_size 64k;
            proxy_temp_file_write_size 64k;
            proxy_max_temp_file_size 128m;
            proxy_pass http://127.0.0.1:8080;
        }

        access_log /data1/app/log/nginx/gamecf.cn.log combined;
        error_log  /data1/app/log/nginx/gamecf.cn-error.log warn;
   }

相关文章:http://www.huilog.com/?p=636

Nginx配置WebSocket反向代理
问题描述

目前项目中需要使用到WebSocket来进行通讯,所以就写了个Nginx反向代理WebSocket的配置文件.

很简单,但是很有效,能够横向扩展WebSocket服务端

先直接展示配置文件,如下(使用的话直接复制,然后改改ip和port即可)

map $http_upgrade $connection_upgrade { 
    default upgrade; 
    '' close; 
} 
upstream wsbackend{ 
    server ip1:port1; 
    server ip2:port2; 
    keepalive 1000; 
} 
 
server { 
    listen 20038; 
    location /{ 
        proxy_http_version 1.1; 
        proxy_pass http://wsbackend; 
        proxy_redirect off; 
        proxy_set_header Host $host; 
        proxy_set_header X-Real-IP $remote_addr; 
        proxy_read_timeout 3600s; 
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
        proxy_set_header Upgrade $http_upgrade; 
        proxy_set_header Connection $connection_upgrade; 
    } 
}

首先:

map $http_upgrade $connection_upgrade { 
    default upgrade; 
    '' close; 
} 
 
表示的是 
1. 如果 $http_upgrade 不为 '' (空),则 $connection_upgrade 为 upgrade 
2. 如果 $http_upgrade 为 '' (空),则 $connection_upgrade 为 close

其次:

upstream wsbackend{ 
    server ip1:port1; 
    server ip2:port2; 
    keepalive 1000; 
} 
 
表示的是 nginx负载均衡 
1. 两台服务器 (ip1:port1)和(ip2:port2) 
2. keepalive 1000 表示的是每个nginx进程中上游服务器保持的空闲连接,当空闲连接过多时,会关闭最少使用的空闲连接.当然,这不是限制连接总数的,可以想象成空闲连接池的大小.设置的值应该是上游服务器能够承受的

最后:

server { 
    listen 20038; 
    location /{ 
        proxy_http_version 1.1; 
        proxy_pass http://wsbackend; 
        proxy_redirect off; 
        proxy_set_header Host $host; 
        proxy_set_header X-Real-IP $remote_addr; 
        proxy_read_timeout 3600s; 
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
        proxy_set_header Upgrade $http_upgrade; 
        proxy_set_header Connection $connection_upgrade; 
    } 
} 
 
表示的是监听的服务器的配置 
1. listen 20038 表示 nginx 监听的端口 
2. locations / 表示监听的路径(/表示所有路径,通用匹配,相当于default) 
3. proxt_http_version 1.1 表示反向代理发送的HTTP协议的版本是1.1,HTTP1.1支持长连接 
4. proxy_pass http://wsbackend; 表示反向代理的uri,这里可以使用负载均衡变量 
5. proxy_redirect off; 表示不要替换路径,其实这里如果是/则有没有都没关系,因为default也是将路径替换到proxy_pass的后边 
6. proxy_set_header Host $host; 表示传递时请求头不变, $host是nginx内置变量,表示的是当前的请求头,proxy_set_header表示设置请求头 
7. proxy_set_header X-Real-IP $remote_addr; 表示传递时来源的ip还是现在的客户端的ip 
8. proxy_read_timeout 3600s; 表的两次请求之间的间隔超过 3600s 后才关闭这个连接,默认的60s.自动关闭的元凶 
9. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 表示X-Forwarded-For头不发生改变 
10. proxy_set_header Upgrade $http_upgrade; 表示设置Upgrade不变 
11. proxy_set_header Connection $connection_upgrade; 表示如果 $http_upgrade为upgrade,则请求为upgrade(websocket),如果不是,就关闭连接

相关文章:
https://blog.csdn.net/l1028386804/article/details/86649543
https://www.cnblogs.com/kevingrace/p/9512287.html

#目录反向代理
location ^~ /abc/
{
    #反向代理地址,最后需要有斜杠
    proxy_pass http://127.0.0.1:8080/tp/public/;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    #代理设置来路地址
    proxy_set_header Referer http://127.0.0.1:8080/tp/public/;
    #代理设置主机地址
    proxy_set_header Host 127.0.0.1:8080;
    proxy_set_header Accept-Encoding "";
    #代理设置页面中替换内容
    sub_filter /tp/public/ /abc/;
    #代理设置页面中批量替换内容,否则只替换第一个
    sub_filter_once off;
}

相关文章:

nginx之location的匹配规则
https://www.cnblogs.com/jiangyang/p/8485046.html