Nginx 完全配置

摘要:
有两种可能的结果:首先,如果发送到Cloudflare的请求中没有现有的“X-Forwarded-For”标头,那么标头将与CF-Connecting-IP标头具有相同的值,如下所示:“X-Forwarded-For:ABCD”其中ABCD是客户端的IP地址,也称为原始访客IP地址。此示例中的XXXX和YYYY是标头值中沿路由的IP地址。CloudFlare还提供了一个cf-connecting-ip专门记录访客IP.如果您对实际的客户端(访问者)IP地址感兴趣,我们建议您依靠CF-Connecting-IP而不是X-Forwarded-For。

入门教程




## 设置静态网页编码 --> 针对非类Unix系统

  • 针对服务器

http {
    ...
    charset  UTF-8;
    ...
    include /etc/nginx/conf.d/*.conf;
}
  • 针对location

server {
    listen       80;
    server_name  _;

    location / {
        charset  UTF-8;
    }
}

## 使 Nginx 自动识别常规文件 mime 类型 conf/mime.types

Nginx 可以根据文件后缀判断 MIME 类型, 避免造成尴尬, 这就是通过包含 mime.types 文件实现的:

include mime.types; # 包含文件 MIME 信息数据库, 实现自动判断
default_type text/html; # 默认 MIME 类型

mime.types 长这个样子:

types {
    text/html                                        html htm shtml;
    text/css                                         css;
    text/xml                                         xml;
    image/gif                                        gif;
    image/jpeg                                       jpeg jpg;
    application/javascript                           js;
    application/atom+xml                             atom;
    application/rss+xml                              rss;

    text/mathml                                      mml;
    text/plain                                       txt;
    text/vnd.sun.j2me.app-descriptor                 jad;
    text/vnd.wap.wml                                 wml;
    text/x-component                                 htc;

    image/png                                        png;
    image/svg+xml                                    svg svgz;
    image/tiff                                       tif tiff;
    image/vnd.wap.wbmp                               wbmp;
    image/webp                                       webp;
    image/x-icon                                     ico;
    image/x-jng                                      jng;
    image/x-ms-bmp                                   bmp;

    application/font-woff                            woff;
    application/java-archive                         jar war ear;
    application/json                                 json;
    application/mac-binhex40                         hqx;
    application/msword                               doc;
    application/pdf                                  pdf;

## 反向代理 proxy_pass , 负载均衡 upstream , 保留请求头 proxy_set_header

upstream这个模块提供一个简单方法来实现在轮询和客户端IP之间的后端服务器负荷平衡。
upstream abc.com {
        server 127.0.0.1:8080;
        server 127.0.0.1:80;
        server 127.0.0.1:8000;
}

server {
        listen 80;
        server_name www.test.com;
        location / {
                proxy_pass http://abc.com;
                proxy_set_header    Host             $host;#保留代理之前的host
                proxy_set_header    X-Real-IP        $remote_addr;#保留代理之前的真实客户端ip, 如果使用了CDN产品, 则是CDN节点IP
                proxy_set_header    X-Forwarded-For  $proxy_add_x_forwarded_for;
                proxy_set_header    HTTP_X_FORWARDED_FOR $remote_addr;#在多级代理的情况下,记录每次代理之前的客户端真实ip
                proxy_redirect      default;#指定修改被代理服务器返回的响应头中的location头域跟refresh头域数值
        }
}
  • $host

nginx反向代理tomcat
proxy_pass http://localhost:8002;
如果不设置header, 那么tomcat收到的请求Host就是
Nginx 完全配置第1张
proxy_set_header Host $host - #保留代理之前的host
Nginx 完全配置第2张

  • $remote_addr

指连接nginx的客户端IP, 有可能是CDN节点IP, 而不是访问者IP.
对于CDN, 行业标准规定x-forwarded-for填充转发IP, X-Forwarded-For是一个完善的HTTP头,代理使用它包括Cloudflare来传递请求中的其他IP地址。这通常与CF-Connecting-IP相同,但请求路径中可能有多层代理。
有两种可能的结果:
首先,如果发送到Cloudflare的请求中没有现有的“X-Forwarded-For”标头,那么标头将与CF-Connecting-IP标头具有相同的值,如下所示:
“X-Forwarded-For:ABCD”
其中ABCD是客户端的IP地址,也称为原始访客IP地址。
其次,如果发送到Cloudflare的请求中存在“X-Forwarded-For”标头,Cloudflare会将HTTP代理的IP地址附加到其值,作为列表中的最后一个。
“X-Forwarded-For:ABCD [,XXXX,YYYY,]”
其中ABCD是客户端的IP地址,也称为原始访客IP地址。此示例中的XXXX和YYYY是标头值中沿路由的IP地址。
CloudFlare还提供了一个cf-connecting-ip专门记录访客IP.如果您对实际的客户端(访问者)IP地址感兴趣,我们建议您依靠CF-Connecting-IP(或True-Client-IP)而不是X-Forwarded-For。
更多CF相关头请查看How does Cloudflare handle HTTP Request headers?
如果不通过head提供转发IP, tomcat就很难知道访问者IP了, request.getRemoteAddr()返回的都是本地回环地址.

  • 实例: 反代Google

server {
        # 虚拟机, 反代google
        listen 80;
        listen [::]:80;
        server_name g.deve.cf google.deve.cf;

        location / {
                proxy_pass https://www.google.com/;
        }
}
server {
        listen 80;
        listen [::]:80;
        server_name deve.cf g.deve.cf google.deve.cf;

        location = / {
                return 500;
        }

        location = /google/ {
                proxy_pass https://www.google.com/;
        }

        location / {
                proxy_pass https://www.google.com/;
        }
}
  • 实例: 反代tomcat子项目

    server {
        listen 80;
        server_name l;

        #rewrite / /git.io/; #

        location / {
                proxy_pass http://localhost:8080/git.io/;
        }

        location /testA {
                proxy_pass http://l/testB;
        }
    }

## [URL重写](https://www.cnblogs.com/develon/p/10778572.html)

重写URL是 Nginx 的重要功能之一, 用于配置基于 location 的虚拟机, 伪静态等, 涉及到正则表达式, 重定向等复杂内容, 重新开一个随笔来研究:


## Nginx 拦截代理错误 proxy_intercept_errors

使用nginx作为前端代理, 若不做特殊处理,当访问出现404时,将会返回tomcat的404页面,现要求拦截错误
使用proxy_intercept_errors on开启拦截代理错误后, 可使用error_page在代理 location 中拦截有关的错误状态, 并自定义错误页面, 注意该页面是正向代理的内部重定向或者一个302跳转.
nginx的配置文件内容如下:

 14         server {
 15                 listen 80;
 16                 server_name abc.com;
 17
 18                 root E:cygwin64homeAdministratorwww;
 19
 20                 proxy_intercept_errors  on; # 开启代理错误拦截
 21                 #recursive_error_pages on; # 递归错误页面(暂时不知道用途, 不过猜测和301 302等错误有关)
 22
 23                 location = /a {
 24                         return http://abc.com/a/index.jsp;
 25                 }
 26
 27                 location /a/ {
 28                         proxy_pass http://tomcat/Shop/;
 29                         proxy_set_header Host $host;
 30                         proxy_set_header X-Real-IP $remote_addr;
 31
 32                         error_page 404 /404;
 33                         proxy_redirect http://$host/Shop/ http://$host/store/;
 34                 }
 35
 36                 location / {
 37                         index index.htm;
 38                         try_files $uri $uri/ $uri.htm =404;
 39                 }
 40
 41                 location = /404 {
 42                         try_files /err.htm =404;
 43                 }
 44         }

配置 SSL



        server {
                listen 80;
                
                listen 443 ssl;    # HTTPS 支持
                ssl_certificate      E:\WebServer\nginx\server.crt;  # 证书crt文件所在目录
                ssl_certificate_key  E:\WebServer\nginx\server.key;  # 证书key文件所在目录
                #ssl_session_cache    shared:SSL:1m;
                #ssl_session_timeout  5m;
                #ssl_ciphers  HIGH:!aNULL:!MD5;
                #ssl_prefer_server_ciphers  on;

                server_name abc.com;
...
        }

拒绝服务



location ~ /(deluser|resetdata)/.*.php$ { 
    deny all; 
}

免责声明:文章转载自《Nginx 完全配置》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇融合趋势下基于 Flink Kylin Hudi 湖仓一体的大数据生态体系Kudu1.1.0 、 Kudu1.2.0 Kudu1.3.0的版本信息异同比较下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Nginx Location匹配规则

Location 语法 语法规则: location [=|~|~*|^~] /uri/ {… } 首先匹配 =,其次匹配^~,其次是按文件中顺序的正则匹配,最后是交给 /通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求。 符号 含义 = = 开头表示精确匹配 ~ ~ 开头表示区分大小写的正则匹配 ~* ~* 开头表示不区分...

Linux服务部署Yapi项目(安装Node Mongdb Git Nginx等) Linux服务部署Yapi

一,介绍与需求  1,我的安装环境:CentOS7+Node10.13.0+MongoDB4.0.10。  2,首先安装wget,用于下载node等其他工具 1 yum install -y wget  编译依赖 gcc 环境 1 yum install gcc-c++ 二,Node安装配置 第一步:下载Node 1 wget https://nodejs...

vue配置请求转发解决跨域问题

通过nodejs的请求转发到后台,前端地址:http://localhost:8080 后端地址:http://localhost:8081 vue.config.js内容如下: let proxyObj={} proxyObj['/']={ //websocket ws:false, target:'http://localho...

Docker环境 ELK 快速部署

Docker环境 ELK快速部署 环境 Centos 7.4 , Docker version 17.12 Docker至少3GB内存; 内核配置 echo ' vm.max_map_count = 262144 net.core.somaxconn=65535 '>>/etc/sysctl.conf sysctl -p #创建elk #下...

Nginx配置同一个域名http与https两种方式都可访问

##配置 http://test.pay.joyhj.com https://test.pay.joyhj.com 两者都可访问 # vim /usr/local/nginx/conf/vhost/test.pay.joyhj.com.confserver{   listen 80;   listen 443 ssl;  ##把ssl on;这行注释掉,...

sshd_config配置详解

sshd_config配置详解 名称sshd_config - OpenSSH SSH 服务器守护进程配置文件 大纲/etc/ssh/sshd_config 描述sshd(8) 默认从 /etc/ssh/sshd_config 文件(或通过 -f 命令行选项指定的文件)读取配置信息。配置文件是由"指令 值"对组成的,每行一个。空行和以'#'开头的行都将被忽...