存档十一月 2019

pptp vpn配置



PPTP VPN


    # modprobe ppp-compress-18 && echo yes   \\ 查看主机否支持pptp,返回结果为yes就表示通过
    # cat /dev/net/tun                        \\ 返回结果为cat: /dev/net/tun: File descriptor in bad state 表示通过 
    # yum install epel-release 
    # yum install ppp pptpd                     \\ pptpd在epel源上
    # vim /etc/pptpd.conf                        \\ pptpd 主配置文件   直接替换
        option /etc/ppp/options.pptpd             \\ 表示启用 /etc/ppp/options.pptpd 文件
        debug                                      \\ 日志追踪 保存位置默认 /var/log/messages
        localip 172.16.0.1                          \\ VPN服务器 内网ip
        remoteip 172.16.0.100-200                    \\ 表示设置一个地址段供客户机连接使用
    # vim /etc/ppp/options.pptpd                      \\ 此文件用于设置DNS  直接替换
        name pptpd
        refuse-pap
        refuse-chap
        refuse-mschap
        require-mschap-v2
        require-mppe-128
        ms-dns 8.8.8.8
        ms-dns 223.5.5.5
        proxyarp
        lock
        nobsdcomp
        novj
        novjccomp
        nologfd
    # vim /etc/ppp/chap-secrets              \\ 用户文件 设置密码
        admin      pptpd     123456     *     \\ *代表随机分配IP地址
    # systemctl restart pptpd                  \\ 启动
    # systemctl enable pptpd                    \\ 开机启动     
    # ss -tnl                                    \\ 1723端口被监控
    # vim /etc/sysctl.conf                        \\ 开启核心转发
        net.ipv4.ip_forward = 1
    # sysctl -p
    # iptables -I INPUT 4 -m state --state NEW -p tcp --dport 1723 -j ACCEPT      \\ 放行1723端口
    # iptables -I INPUT 4 -p gre -j ACCEPT                                         \\ 还需要放行 gre  47
    # iptables -I FORWARD -s 172.16.0.0/24 -j ACCEPT                                \\ 转发入方向 
    # iptables -I FORWARD -d 172.16.0.0/24 -j ACCEPT                                 \\ 转发出方向
    # iptables -t nat -I POSTROUTING -s 172.16.0.0/24 -o ens33 -j MASQUERADE          \\ 注意网卡名字 通过哪个网卡转发
    # iptables -L -n
    # iptables -t nat -L -n
    # service iptables save



如使用firewalld防火墙 可关闭 换成iptables 命令如下
    # systemctl disable firewalld.service      \\ 关闭firewalld 开机启动
    # systemctl stop firewalld.service          \\ 停止服务  停止后iptables会被清空
    # yum install iptables-services              \\ 安装iptables防火墙
    # systemctl restart iptables
    # iptables-restore < /data/iptables.bak        \\ 导入已保存的 iptables规则文件
    # systemctl enable iptables                     \\ 开机启动


PPTP服务端与客户端 修改默认PPTP默认端口1723
    linux pptp服务端:
    我们在Linux下建立的pptpd端口号默认是1723,有时候这个端口并不是那么的好用,不是麽?
    所以服务端修改端口号比较简单
    修改 /etc/services 文件
    查找 1723,然后将其修改为你想修改的数值,重启 pptpd即可.


Windows PPTP客户端:
    1 找到 C:\WINDOWS\system32\drivers\etc,修改services文件,修改里面的VPN(PPTP)端口1723为你设定的端口
    2 进入HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE- BFC1-08002bE10318}项,
      其中有类似0000、0001、0002……这样的子项,每个子项都对应一个网适配器的配置。逐一打开这些子项,找到其中字段DriverDesc
      值为WAN Miniport (PPTP)的子项,例如我找到的是0003。在这个子项里的TcpPortNumber的值就是pptp vpn所使用的端口,双击
      修改其值,选择基数为十进制,修改成所需要的值确认即可。重启机器后,修改生效。


PPTP简介
    PPTP,Point to Point Tunneling Protocol,点对点隧道协议。
    这是一种支持多协议虚拟专用网络(VPN)技术。
    远程用户能够通过装有点对点协议的系统安全访问公司网络。
    PPTP使用GRE的扩展版本来传输用户PPP包。
    这些增强允许为在PAC和PNS之间传输用户数据的隧道提供底层拥塞控制和流控制。
    这种机制允许高效使用隧道可用带宽并且避免了不必要的重发和缓冲溢出。
    PPTP没有规定特定的算法用于低层控制,但它确实定义了一些通信参数来支持这样的算法工作。



Nginx 服务器




Nginx 服务器


一. Nginx 安装

    ❶. 使用 yum 安装 Nginx

        # cat > /etc/yum.repos.d/nginx1.repo <<-EOF
            [nginx-stable]
            name=nginx stable repo
            baseurl=http://nginx.org/packages/centos///$releasever///$basearch/
            gpgcheck=1
            enabled=1
            gpgkey=https://nginx.org/keys/nginx_signing.key
            module_hotfixes=true
            EOF
        # yum install nginx
        # systemctl restart nginx         // 如果更换默认的 
        # ss -tnl


    ❷. 编译安装 Nginx

        # yum install openssl openssl-devel gcc gcc-c++ pcre pcre-devel
        # groupadd www
        # useradd -g www www -s /sbin/nologin
        # wget http://nginx.org/download/nginx-1.21.0.tar.gz
        # tar zxvf nginx-1.21.0.tar.gz
        # cd nginx-1.21.0                 // 安装完成 此目录可以删除
        # ./configure --prefix=/usr/local/nginx --pid-path=/run/nginx.pid --with-http_ssl_module --with-http_realip_module --with-http_v2_module --with-stream --with-http_gzip_static_module --with-http_flv_module --with-http_mp4_module --with-file-aio --with-http_stub_status_module --with-pcre
        # make && make install
        # /usr/local/nginx/sbin/nginx               // 启动
        # ss -tnl                                   // 80端口被监控 注意 防火墙放行 关闭selinux
        # /usr/local/nginx/sbin/nginx -V            // 查看nginx 版本 及 编译参数
        # /usr/local/nginx/sbin/nginx -s reload     // 重新加载
        # /usr/local/nginx/sbin/nginx -s stop       // 停止

        # vim /etc/init.d/nginx                           // 新建启动脚本  见附录 1  或 导入即可   ... ....
        # chmod +x /etc/init.d/nginx                      // 添加执行权限  
        # chkconfig --add /etc/init.d/nginx               // 添加到service 管理 
        # chkconfig nginx on                              // 添加开启启动
        # ln -s /usr/local/nginx/sbin/nginx /sbin/nginx   // 软连接到 系统 环境中
        # service nginx start                             // 启动
        # systemctl restart nginx                         // 重启
        # ss -tnl                                         // 80端口被监听

        # cd /usr/local/nginx/conf
        # mv nginx.conf nginx.conf.bak           // 备份默认的配置文件
        # vim /usr/local/nginx/conf/nginx.conf   // 新建以优化的配置文件  见附录 1  或  导入即可   ... ....
        # mkdir -p /usr/local/nginx/conf/sites
        # systemctl restart nginx                // 重启
        # ss -tnl

    ❸. 已经运行的 nginx 中 添加 新模块
        # wget http://nginx.org/download/nginx-1.21.0.tar.gz
        # tar zxvf nginx-1.21.0.tar.gz
        # cd /usr/local
        # tar zcvf nginx.bak.tar.gz nginx/   // 备份nginx  文件
        # cd /data/nginx-1.21.0              // 需要 源码 目录
        # nginx -V                           // 查看编译的模块 在后面添加 新的编译内容
        # mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak    // 备份之前的 启动文件
        # ./configure --prefix=/usr/local/nginx --pid-path=/run/nginx.pid --with-http_ssl_module --with-http_realip_module --with-http_v2_module --with-stream --with-http_gzip_static_module --with-http_flv_module --with-http_mp4_module --with-file-aio --with-http_stub_status_module --with-pcre --with-stream_ssl_module
        # make                               // 只能 make   不能 instsll 会覆盖安装
        # cp /data/nginx-1.21.0/objs/nginx /usr/local/nginx/sbin/nginx
        # /usr/local/nginx/sbin/nginx -t
        # systemctl restart nginx


------------------------------------------------------------------------------------------------------------------------


二. Nignx 各种配置文件

    ❶. Nginx 301 跳转 配置

        server{
            listen 80;
            server_name ssyy99.com www.ssyy99.com;
            rewrite ^ https://www.baidu.com permanent;

        }

        server{
            listen 443 ssl http2;
            server_name ssyy99.com www.ssyy99.com;
            access_log /home/logs/ssyy99.com_access.log;
            error_log /home/logs/ssyy99.com_error.log;
            ssl_certificate    /etc/letsencrypt/live/ssyy99.com/fullchain.pem;
            ssl_certificate_key    /etc/letsencrypt/live/ssyy99.com/privkey.pem;
            ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
            ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
            ssl_prefer_server_ciphers on;
            ssl_session_cache shared:SSL:10m;
            rewrite ^ https://www.baidu.com permanent;

        }

    ❷. Nginx 反向代理 配置    -->  反向代理到本机 某一个端口

        server {
            listen 80;
            server_name m.ssyy99.com;
            location / {
                proxy_next_upstream off;
                proxy_connect_timeout       10s;
                proxy_send_timeout          10s;
                proxy_read_timeout          10s;
                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_pass http://127.0.0.1:8023;
            }
        }

    ❸. Nginx 反向代理 配置    -->   http 强制跳转 https 反向代理到后端的 http

        upstream ssyy99.com {
            ip_hash;
            server 47.91.219.64;
        }

        server {
            listen 80;
            add_header Strict-Transport-Security max-age=15768000;
            server_name ssyy99.com www.ssyy99.com;
            rewrite ^/(.*) https://$server_name$1 permanent;
        }       

        server {
            listen 443 ssl http2;
            server_name ssyy99.com www.ssyy99.com;
            add_header Content-Security-Policy upgrade-insecure-requests;
            access_log /usr/local/nginx/logs/ssyy99.com_access.log;
            error_log /usr/local/nginx/logs/ssyy99.com_error.log;
            ssl_certificate    /etc/letsencrypt/live/ssyy99.com/fullchain.pem;
            ssl_certificate_key    /etc/letsencrypt/live/ssyy99.com/privkey.pem;
            location / {
                proxy_redirect off;
                proxy_set_header        Host            $host;
                proxy_set_header        X-Real-IP       $remote_addr;
                proxy_set_header        REMOTE_ADDR     $remote_addr;
                proxy_set_header        HTTP_CLIENT_IP  $remote_addr;
                proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
                client_max_body_size 20m;
                client_body_buffer_size 256k;
                proxy_connect_timeout   300;
                proxy_send_timeout      300;
                proxy_read_timeout      300;
                proxy_pass http://ssyy99.com;
            }
            location ~ //.(css|js|gif|png|jpg|swf|bmp|swf|flv|ico)$ {
                proxy_redirect off;
                proxy_set_header        Host            $host;
                proxy_set_header        X-Real-IP       $remote_addr;
                proxy_set_header        REMOTE_ADDR     $remote_addr;
                proxy_set_header        HTTP_CLIENT_IP  $remote_addr;
                proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
                client_max_body_size 20m;
                client_body_buffer_size 256k;
                proxy_connect_timeout   300;
                proxy_send_timeout      300;
                proxy_read_timeout      300;
                proxy_pass http://ssyy99.com;
            }
            location ~ .*//.(php|php5)?$ {
                proxy_redirect off;
                proxy_set_header        Host            $host;
                proxy_set_header        X-Real-IP       $remote_addr;
                proxy_set_header        REMOTE_ADDR     $remote_addr;
                proxy_set_header        HTTP_CLIENT_IP  $remote_addr;
                proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
                client_max_body_size 20m;
                client_body_buffer_size 256k;
                proxy_connect_timeout   300;
                proxy_send_timeout      300;
                proxy_read_timeout      300;
                proxy_pass http://ssyy99.com
            }
            location ~ .*//.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
                expires 30d;
            }
            location ~ .*//.(js|css)?$ {
                expires 7d;
            }
        }


        注: 
            1. add_header Strict-Transport-Security max-age=15768000;          // 只使用https访问  强制跳转到https的时候加上此项
            2. add_header Content-Security-Policy upgrade-insecure-requests;   // https反向代理http时候 加载静态资源
            3. 如果 反向代理到后端的 https 
                proxy_pass https://ssyy99.com;
                server 47.91.219.64:443;


    ❹. Nginx 部署 php 网页配置文件

        ①. Nginx 部署 php 网页配置文件    -->   不强制跳转

            server {
                listen 80;
                server_name ssyy99.com www.ssyy99.com;
                access_log /home/logs/ssyy99.com_access.log;
                error_log /home/logs/ssyy99.com_error.log;

                location / {
                    root /vv/aoyoufront/public;
                    index index.html index.htm index.php;
                    try_files $uri $uri/ /index.php?$query_string;
                }

                location ~ //.php$ {
                    root           /vv/aoyoufront/public;
                    fastcgi_pass   127.0.0.1:9000;
                    fastcgi_index  index.php;
                    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
                    include        fastcgi_params;
                }

            }

            server {
                listen 443 ssl http2;
                server_name ssyy99.com www.ssyy99.com;
                access_log /home/logs/thwav.vip_access.log;
                error_log /home/logs/thwav.vip_error.log;
                ssl_certificate     /etc/letsencrypt/live/ssyy99.com/fullchain.pem;
                ssl_certificate_key /etc/letsencrypt/live/ssyy99.com/privkey.pem;
                ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
                ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
                ssl_prefer_server_ciphers on;
                ssl_session_cache shared:SSL:10m;

                location / {
                    root /data/www/ssyy99.com;
                    index index.html index.htm index.php;
                    try_files $uri $uri/ /index.php?$query_string;
                }

                location ~ //.php$ {
                    root           /vv/aoyoufront/public;
                    fastcgi_pass   127.0.0.1:9000;
                    fastcgi_index  index.php;
                    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
                    include        fastcgi_params;
                }

            }


        ②. Nginx 部署 php 网页配置文件    -->    http 强制跳转 https

            server {
                listen 80;
                add_header Strict-Transport-Security max-age=15768000;
                server_name ssyy99.com www.ssyy99.com;
                rewrite ^/(.*) https://$server_name$1 permanent;
            }

            server {
                listen 443 ssl http2;
                server_name ssyy99.com www.ssyy99.com;
                access_log /home/logs/ssyy99.com_access.log;
                error_log /home/logs/ssyy99.com_error.log;
                ssl_certificate     /etc/letsencrypt/live/ssyy99.com/fullchain.pem;
                ssl_certificate_key /etc/letsencrypt/live/ssyy99.com/privkey.pem;
                ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
                ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
                ssl_prefer_server_ciphers on;
                ssl_session_cache shared:SSL:10m;

                location / {
                    root /data/www/ssyy99.com;
                    index index.html index.htm index.php;
                    try_files $uri $uri/ /index.php?$query_string;
                }

                location ~ //.php$ {
                    root           /vv/aoyoufront/public;
                    fastcgi_pass   127.0.0.1:9000;
                    fastcgi_index  index.php;
                    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
                    include        fastcgi_params;
                }

            }


    ❺. Nginx 判断 是否为手机端

        server {
            listen 80;
            server_name www.ssyy99.com;
            #server_name 47.91.219.64;
            charset utf-8;
            access_log /data/wwwlogs/www.ssyy99.com.log combined;

            location / {
                set $is_mobile false;
                if ( $http_cookie ~* "ACCESS_TERMINAL=mobile" ) {
                        set $is_mobile true;
                   }
                if ($http_user_agent ~* (android|ip(ad|hone|od)|kindle|blackberry|windows//s(ce|phone))) {
                        set $is_mobile true;
                   }
                if ($is_mobile = true) {
                root /data/www/www.ssyy99.com;
                        break;
                   }
                root /data/www/ssyy99.com;
                try_files $uri $uri/ /index.html;
                index index.html Idex.html index.htm;
                }
        }

        注:
            关于 try_files
                如请求 http://www.ssyy99.com/images/aaa.jpg    查找路径是按照给定的root或alias为根路径来查找的
                $uri   会查找返回 /data/www/www.ssyy99.com/images/aaa.jpg
                $uri/  会查找返回 /data/www/www.ssyy99.com/images/aaa.jpg/ 下的 index.html文件
                       如果不写上 $uri/  当直接访问一个目录路径时 并不会去匹配目录下的索引页
                       即 访问www.ssyy99.com/images/ 不会去访问  www.ssyy99.com/images/index.html
                /index.html 会查找返回 /data/www/www.ssyy99.com/  下的 index.html文件
            try_files 其他用法
                location / {
                    try_files /system/maintenance.html
                        $uri $uri/index.html $uri.html
                        @www.ssyy99.com;
                }

                location @www.ssyy99.com {
                    proxy_pass http://www.ssyy99.com;
                }
                以上中若未找到给定顺序的文件,则将会交给location @www.ssyy99.com处理(相当于匹配到了@www.ssyy99.com来匹配)


    ❻. Nginx 判断手机 苹果或安卓 返回不同的链接 可以用在 多个二维码 合成 一个

        server {
                listen 80;
                add_header Strict-Transport-Security max-age=15768000;
                server_name ssyy99.com;
                rewrite ^/(.*) https://$server_name/$1 permanent;

        }

        server {
                listen 443 ssl;
                server_name ssyy99.com;
                access_log /home/logs/ssyy99.com_access.log;
                charset utf-8;
                ssl_certificate /etc/letsencrypt/live/ssyy99.com/fullchain.pem;
                ssl_certificate_key /etc/letsencrypt/live/ssyy99.com/privkey.pem;
                ssl_session_timeout 5m;
                ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
                ssl_prefer_server_ciphers on;

                location / {
                    #微信
                    #if ($http_user_agent ~ "MicroMessenger"){
                    #     return 301 http://a.app.qq.com/o/simple.jsp?pkgname=com.baohuquan.share; #腾讯应用宝地址
                    #}

                    #iPhone设备
                    if ($http_user_agent ~ "iPhone"){
                        return 301 itms-services://?action=download-manifest&url=https://m.ssyy99.com/ssyy99.plist;#AppStore
                    }

                    #安卓设备
                    if ($http_user_agent ~ "Android"){
                         return 301 https://m.videowxr.com/huarenyulev2.apk;
                    }

                    #浏览器访问
                    if ($http_user_agent ~ "(Mozilla)|(IE)|(Chrome)|(Safari)"){
                         return 301 itms-services://?action=download-manifest&url=https://m.ssyy99.com/ssyy99.plist;
                    }

                }

        }


    ❼. nginx 跨域

        server {
            listen 443 ssl;
                server_name ssyy99.com www.ssyy99.com;
                charset utf-8;
                ssl_certificate /etc/letsencrypt/live/ssyy99.com/fullchain.pem;
                ssl_certificate_key /etc/letsencrypt/live/ssyy99.com/privkey.pem;
                ssl_stapling on;
                ssl_session_timeout 5m;
                ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
                ssl_prefer_server_ciphers on;
                location / {
                    if ($request_method = 'OPTIONS') {
                        add_header 'Access-Control-Allow-Origin' '*';
                        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,x-token,X-Token,Version,Source,version,source,Client-Time,client-time';
                        # Tell client that this pre-flight info is valid for 20 days
                        add_header 'Access-Control-Max-Age' 1728000;
                        add_header 'Content-Type' 'text/plain charset=UTF-8';
                        add_header 'Content-Length' 0;
                        return 204;
                    }
                    if ($request_method = 'POST') {
                        add_header 'Access-Control-Allow-Origin' '*';
                        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,x-token,X-Token,Version,Source,version,source,Client-Time,client-time';
                     }
                    if ($request_method = 'GET') {
                        add_header 'Access-Control-Allow-Origin' '*';
                        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,x-token,X-Token,Version,Source,version,source,Client-Time,client-time';
                     }
                    proxy_set_header     Host      $host ;
                    proxy_pass http://127.0.0.1:8101 ;
                }

               location /tutu/ {
                    if ($request_method = 'OPTIONS') {
                        add_header 'Access-Control-Allow-Origin' '*';
                        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,x-token,X-Token,Version,Source,version,source,Client-Time,client-time';
                        # Tell client that this pre-flight info is valid for 20 days
                        add_header 'Access-Control-Max-Age' 1728000;
                        add_header 'Content-Type' 'text/plain charset=UTF-8';
                        add_header 'Content-Length' 0;
                        return 204;
                    }
                    if ($request_method = 'POST') {
                        add_header 'Access-Control-Allow-Origin' '*';
                        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,x-token,X-Token,Version,Source,version,source,Client-Time,client-time';
                     }
                    if ($request_method = 'GET') {
                        add_header 'Access-Control-Allow-Origin' '*';
                        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,x-token,X-Token,Version,Source,version,source,Client-Time,client-time';
                    }
                    proxy_set_header     Host      $host ;
                    proxy_pass http://127.0.0.1:8100/tuotu/ ;
                }
        }

        注: 
            ①. add_header 'Access-Control-Allow-Origin' '*';     // 服务器默认是不被允许跨域的  此选项接受所有跨域的请求

            ②. add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; 
               // 解决报错 Content-Type is not allowed by Access-Control-Allow-Headers in preflight response.

            ③. add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With ... ...;  
              // 解决报错 Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight response

            ④. add_header 'Access-Control-Max-Age' 1728000; // 许缓存该条回应1728000秒(即20天) 在此期间 不用发出另一条预检请求

            ⑤. return 204;          // 给OPTIONS 添加 204的返回,是为了处理在发送POST请求时Nginx依然拒绝访问的错误

        跨域实例01: 在网址 https://ssyy99.com/ 中 有个图片链接为 http://baidu.com:939/wetsite/a.jpg 
                  在大多数浏览器打开 https://ssyy99.com/ 不会显示 a.jpg 这个图片 会报如下错误  这就产生了跨域问题
                    Mixed Content: The page at https://ssyy99.com/' was loaded over HTTPS, but requested an insecure 
                    element 'http://baidu.com:939/wetsite/a.jpg'. This request was automatically upgraded to HTTPS,
                    For more information see https://blog.chromium.org/2019/10/no-more-mixed-messages-about-https.html
            解决: 在引用链接的域名上配置 在 http://baidu.com nginx的 location /{  ...  } 加 跨域代码 可解决此问题


    ❽. Nginx 配置 http / https 代理                 // 需要 重新 编译 nginx

        # yum install openssl openssl-devel gcc gcc-c++ pcre pcre-devel patch
        # groupadd www
        # useradd -g www www -s /sbin/nologin
        # wget http://nginx.org/download/nginx-1.21.0.tar.gz
        # tar zxvf nginx-1.21.0.tar.gz
        # git clone https://github.com/chobits/ngx_http_proxy_connect_module.git              // 需单独下载 代理模块
        # cd nginx-1.21.0                                                                     // 安装完成 此目录可以删除
        # patch -p1 < ../ngx_http_proxy_connect_module/patch/proxy_connect_rewrite_1018.patch // 打补丁
        # ./configure --prefix=/usr/local/nginx --pid-path=/run/nginx.pid --with-http_ssl_module --with-http_realip_module --with-http_v2_module --with-stream --with-http_gzip_static_module --with-http_flv_module --with-http_mp4_module --with-file-aio --with-http_stub_status_module --add-module=../ngx_http_proxy_connect_module
        # make && make install

        # vim /usr/local/nginx/conf/sites/proxy.conf
            server {
                listen 8000;
                # dns resolver used by forward proxying
                resolver 8.8.8.8;

                # forward proxy for CONNECT request
                proxy_connect;
                proxy_connect_allow            443 563;
                proxy_connect_connect_timeout  10s;
                proxy_connect_read_timeout     10s;
                proxy_connect_send_timeout     10s;

                # forward proxy for non-CONNECT request
                location / {
                    proxy_pass http://$host;
                    proxy_set_header Host $host;
                }
            }

        # /usr/local/nginx/sbin/nginx         // 启动
        # ss -tnl                             // 8000端口被监听  如需 systemctl 管理 详见 nginx 编译安装

        # curl https://baidu.com/ -v -x 47.91.219.64:8000       // 随便一台机器 测试代理

        注:
            nginx正向代理模块开源地址 https://github.com/chobits/ngx_http_proxy_connect_module


------------------------------------------------------------------------------------------------------------------------


三、 Nginx的 常用配置

    ❶. 关于 websocket

        ①. websocket 测试网址 http://coolaf.com/tool/chattest
            ws://ssyy99.com:9501       -->   http://ssyy99.com:9501
            wss://ssyy99.com/dev       -->   https://ssyy99.com/dev
            wss 就是 ws 基于 SSL 的安全传输
            
        ②. wss://ssyy99.com/dev/  跳  ws://ssyy99.com:9501   -->  https://ssyy99.com/dev/  跳  http://ssyy99.com:9501  


    ❷. 关于nginx ip 的限制 及 阻止国外ip 访问

        在与主配置文件同级目录下 创建blockip.conf文件 配置规则即可 
        使用 include blockip.conf; 可在http段全局做控制也可以在server段做单个网址控制
            blockip.conf 
                allow 172.0.0.1;
                allow 192.168.10.10;
                allow 192.168.10.0/24;
                deny all;
        所有国家ip段下载: https://www.ipdeny.com/ipblocks/
        # cat ./* > blockip.conf                  // 合并多个文件
        # sed -i 's/.*/allow &;/g' blockip.conf   // 添加类似 allow 192.168.10.0/24; 规则 


    ❸. 关于 Nginx localtion 匹配顺序

        localtion {}  的匹配规则

            location [ = | ~ | ~* | ^~ ] uri { … }
                ~  匹配
                ~* 匹配        忽略大小写
                ^~ 行首牟定
                =  精确匹配
                //. 转义.
                $  行尾牟定
                ?  匹配0个或1个字符    

            location ~ /data/   只要目录里面有 /data/ 就可以
            location ~* /data/  同上 不区分大小写
            location ^~ /data/  以 /data/开头的  比下面优先匹配
            location /data/     以 /data/开头的  匹配顺序在 正则之后
            location /          匹配所有 但是正则和最长字符串会优先匹配
            location ~ //.php$                转义
            location ~ ///.ht                 匹配 /.ht  开头的
            location ~ .*//.(js|css)?$
            location ~ .*//.(php|php5)?$
            location ~* //.(gif|jpg|jpeg|png|css|js|ico)$
            location ~ //.(css|js|gif|png|jpg|swf|bmp|swf|flv|ico)$

        localtion {} 匹配顺序

            (location =) > (location 完整路径) > (location ^~ 路径) > (location ~,~* 正则顺序) > (location 部分起始路径) > (/)

            location /www/ {        这里 最后一定要加 /
                root /data;         root的处理结果是:root路径+location路径      root最后的分号前一定不加 /     
            }

            location /a/ {
                alias /data/b/;     alias的处理结果是:使用alias路径替换location路径    alias后面必须要用"/"结束
            }


    ❹. 关于 Nginx 重定向

        rewrite ^/(.*) https://$server_name$1 permanent;       // 301重定向 强制 使用https 访问网页
        rewrite ^ http://www.ssyy99.com permanent;             // 301永久 重定向
        rewrite ^ http://www.ssyy99.com redirect;              // 302
        rewrite ^ http://www.ssyy99.com;                       // 302   
        return http://google.com;
        return 301 http://google.com;                               // 301永久 重定向

        rewrite ^/(.*)$ http://www.ssyy99.com/$1;                   // 302 后向引用 引用前面的地址 
        rewrite ^ http://www.ssyy99.com$request_uri? redirect;      // 302临时 重定向 引用前面的地址
        return 302 http://www.a1v1.com$request_uri;                 // 302永久重定向 地址引用

        301重定向和302重定向的区别
        302重定向只是暂时的重定向 搜索引擎会抓取新的内容而保留旧的地址 因为服务器返回302 所以 搜索搜索引擎认为新的网址是暂时的。
        301重定向是永久的重定向 搜索引擎在抓取新的内容的同时也将旧的网址替换为了重定向之后的网址。


    ❺. 下载页面

        location /download {
            charset utf-8;                          // 有中文需要设置 否则乱码
            alias /data/download/;
            sendfile on;                            // 使用sendfile系统调用来传输文件
            autoindex on;                           // 目录作为根目录来直接列出来
            autoindex_exact_size off;               // 显示出文件的大概大小 单位是kB或者MB或者GB
            autoindex_localtime on;                 // 显示的文件时间为文件的服务器时间
        }

        注: 
            Nginx 下载的文件 相关
                .apk 和 .ipa分别是android应用和ios应用的扩展名,其本身就是压缩包。而大部分用户的电脑
                上都安装了WinRAR的解压缩软件。果在浏览器下载这些后缀的文件时,会自动重命名为zip文件。
                    当然可以下载后手动修改后缀,依然可以安装。

                如果想下载后缀为apk ipa 的文件,可以修改 conf/mime.types文件,加入以下内容:
                    application/vnd.android.package-archive apk;
                application/iphone          pxl ipa;


    ❻. 关于 Nginx 报错 error.log" failed (24: Too many open files)

        # ulimit -n                       // 可查看连接数
        # vim /etc/security/limits.conf   // 加到最后即可  重开shell 可查看到修改后的数值
            * soft nofile 1024000
            * hard nofile 1024000
            hive   - nofile 1024000
            hive   - nproc  1024000
            # End of file
        # nginx -t


    ❼. 关于nginx强制使用https访问(http跳转到https)

        1 可以使用 rewrite 方法                // 见上面配置
        2 可以使用 error 497 方法              // 见上面配置
        3 可以使用 网页跳转
            server {  
                listen 192.168.1.11:80;  
                server_name test.com;  
                location / {  
                    root /data/www/test.com/;          #index.html放在虚拟主机监听的根目录下  
                }  
                error_page  404 https://test.com/;         #将404的页面重定向到https的首页  
            }  
            # vim /data/www/test.com/index.html
                <html>  
                <meta http-equiv="refresh" content="0;url=https://test.com/">  
                </html>


    ❽. 关于 Nginx 错误页面

        ①. 404 错误页面
            server {
                error_page  404     /404.html;
                location = /404.html {
                    root /data/www;                             // 在/data/www/下面 创建 404.html
                }
            }
        ②. 自定义 错误码
            server {
                error_page   500 502 503 504 =200  /50x.html;    // 自定义错误代码200   跳转自定义页面
                location = /50x.html {
                    root   /data/www;
                }
            }

        ③. 错误也没 直接 跳转
            error_page 404 500 501 = http://www.baidu.com;      // 以302 跳转 页面

        ④. 禁止ip访问 直接返回500 并使用error_page 重定向
            server {
                listen 80 default;
                server_name _;
                return 500; 
                error_page 404 500 501 = http://www.ssyy99.com;  // 以302 跳转 页面
            }

        ⑤. 禁止ip访问 返回错误码 500
            server {
                listen 80 default;
                server_name _;
                return 500;
        }


    ❾. nginx 中 配置文件 颜色
        # mkdir -p /root/.vim/syntax
        # wget http://www.vim.org/scripts/download_script.php?src_id=14376 -O /root/.vim/syntax/nginx.vim
        # echo "au BufRead,BufNewFile /usr/local/nginx/* set ft=nginx" > /root/.vim/filetype.vim           // nginx 目录


------------------------------------------------------------------------------------------------------------------------


四. Nginx 主配置文件详解

user  www;                      // 定义Nginx运行的用户和用户组
worker_processes  16;           // worker进程数: 通常应该略少于CPU物里核心数

pid        logs/nginx.pid;      // 指定nginx守护进程的pid文件 pid记录了该进程的ID
worker_rlimit_nofile 30000;     // 指定所有worker进程所能够打开的最大文件句柄数 最大打开的文件数 有必要添加 默认系统限制1024 

events {                        // 工作模式与连接数上限
    use epoll;                  // 高性能方式 仅用于linux2.6以上内核,可以大大提高nginx的性能
    worker_connections  30000;  // 设定单个worker进程所能够处理的最大并发连接数量 
                                // 一般 数值 * worker_processes 小于设定的worker_rlimit_nofile 65535 就可以
}

http {                                        // 设定http服务器
    include       mime.types;                 // 文件扩展名 与 文件类型映射表
    default_type  application/octet-stream;   // 默认文件类型
    log_format main '$server_name $remote_addr $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent $bytes_sent "$http_referer" '
                    '$request_time $upstream_response_time $pipe '
                    '"$http_user_agent" "$http_x_forwarded_for"';
    server_tokens off;                   // 隐藏 nginx 版本号 默认on 返回错误页面时是否在Server中注明Nginx版本
    server_names_hash_bucket_size 128;   // 虚拟主机名多的情况下 配置 为了提高快速寻找到相应server name的能力
                                         // ↑ Nginx使用散列表来存储server name 设置了每个散列桶占用的内存大小
    sendfile        on;                  // 开启高效传输模式  提高了发送文件的效率 如果图片显示不正常把这个改成off 详见注解01
    client_max_body_size 50m;            // nginx能处理的最大请求主体大小 默认1m  注解03
    client_body_buffer_size        256k; // 请求主体的缓冲区大小 默认8k/16K 缓冲区代理缓冲用户端请求的最大字节数
    client_header_buffer_size 256k;      // 为请求头分配一个缓冲区 如果大于缓冲区 使用large_client_header_buffers分配更大缓冲区
    large_client_header_buffers 4 256k;  // 读取大型客户端请求头的缓冲区的最大数量和大小  这些缓冲区仅
                                         // ↑ 在缺省缓冲区不足时按需分配 当处理请求或连接转换到保持活动状态时 释放缓冲区。
    tcp_nopush on;            // 防止网络阻塞 将会在发送响应时把整个响应包头放到一个TCP包中发送 积极的作用是减少网络报文段的数量
    keepalive_timeout  300;   // 长连接超时时间 单位是秒
    tcp_nodelay on;           // 默认为on  防止网络阻塞 内核会等待将更多的字节组成一个数据包 从而提高I/O性能
    ssi on;               // 开启ssi支持 默认是off  服务器端嵌入 是一种类似于ASP的基于服务器的网页制作技术
    ssi_silent_errors on; // 默认off 在处理SSI文件出错时不输出错误提示:"[an error occurred while processing the directive]"
    ssi_types text/shtml; // 默认text/html 如果需要htm和html支持 则不需要设置 如需要shtml支持则设置ssi_types text/shtml
    gzip  on;             // 开启gzip压缩输出  页面使用gzip 压缩 提高页面加载速度 一般只用这一项就可以
    gzip_min_length  1k;     // 最小压缩文件大小  建议设置成大于1K,如果小于1K可能会越压越大
    gzip_buffers     4 16k;  // 压缩缓冲区大小申请4个单位16K内存作为压缩流缓存 默认是申请与原始数据相同空间来存储gzip压缩结果
    gzip_http_version 1.0;   // 压缩版本 默认1.1 前端如果是squid2.5请使用1.0
    gzip_comp_level 2;       // 压缩比率 一般为设置为6  1压缩比最小 处理速度最快 9压缩比最大 传输速度快但处理最慢比较消耗cpu
    gzip_types text/plain application/x-javascript text/css application/xml text/javascript; 
                                  // ↑ 压缩类型 默认已经包含text/html 所以下面就不用再写了 写上去也不会有问题 但是会有一个warn
    gzip_vary on;                 // 如果gzip是活动的 该指令启用或者禁用包含Vary: Accept-Encoding 头的响应  建议开启
    fastcgi_connect_timeout 300;  // 连接到后端fastcgi超时时间
    fastcgi_send_timeout 300;     // 该指令指定在连接关闭之前从FastCGI 服务器两次成功写操作
    fastcgi_read_timeout 300;     // 该指令指定在连接关闭之前从FastCGI 服务器两次成功读操作
    fastcgi_buffer_size 64k;      // 该指令为来自FastCGI 服务器响应头的第一部分设置缓冲大小
    fastcgi_buffers 4 64k;            // 该指令设置来自FastCGI 服务器响应的缓冲数量和大小 用于单个连接
    fastcgi_busy_buffers_size 128k;   // 该指令指定缓冲区总的大小 它们都分配给发送响应至客户端使用 同时仍从FastCG 读取服务器
                                      // 典型的设置是将其值设置为fastcgi_buffers 的两倍  
                                      // 以上参数目的 FastCGI相关参数是为了改善网站的性能:减少资源占用 提高访问速度
    proxy_buffer_size 256k;           // 设置代理服务器(nginx)保存用户头信息的缓冲区大小
    proxy_buffers 4 256k;             // 该指令指定用于响应上游服务器的缓冲数量和大小
    proxy_busy_buffers_size 256k;     // 高负荷下缓冲大小(proxy_buffers*2)
                                      // 重用Session提高https性能
    ssl_dhparam /usr/local/nginx/ssl/dhparam.pem;   // 填写此前生成的 “dhparam.pem” 文件路径  可以删除此项 不影响ssl的定义
    ssl_session_cache shared:SSL:10m;               // 该指令指定SSL 缓存以及其是否应该在所有的worker 进程中共享 详见注解03
    ssl_session_timeout 10m;                        // 指定客户端可以重用会话参数的时间(超时后不可使用)
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;            // 允许 ssl 协议的类型
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:... ... // 此处省略一堆码 详见配置文件nginx.conf   此条目为 加密算法
    ssl_prefer_server_ciphers on;            // 该指令表明SSLv3 和TLSvl 服务器密码是客户端的首选 本条目 启用加密算法
    ssl_stapling on;                         // 如报错 可删除此项目 提高 TLS 握手效率 详见注解04
    ssl_stapling_verify on;                  // 提高 TLS 握手效率 启用或禁用服务器验证OCSP响应 OCSP是在线证书状态协议
    resolver 8.8.8.8 8.8.4.4 114.114.114.114 223.5.5.5 valid=300s; // 防止DNS欺骗建议在正确安全的受信任本地网络配置DNS服务器
    resolver_timeout 10s;                                          // 设置名称解析的超时时间 
    error_page 497 "https://$host$uri?$args";   // 让http请求重定向到https请求 当此站点只允许https访问时 用http访问时会报出   
                                                // ↑ 497错误码 利用error_page将497状态码的链接重定向到https://*.com域名上      
    add_header Strict-Transport-Security max-age=63072000;     // 详见注解01
    add_header X-Content-Type-Options nosniff;     // 禁用浏览器内容嗅探 如网站是博客论坛等是为了失效某些浏览器的内容类型探嗅
    server {                                       // 服务器 设置
        listen 443 default_server ssl;             // 监听端口 默认服务器 关于default_server详解 见注解06
        server_name _;                             // 防止ip访问 它只是一个无效的域名   从来不会匹配任何真实名字相匹配
        ssl_certificate /usr/local/nginx/ssl/ssl.crt;     // ip访问时https ssl服务的证书 存放位置
        ssl_certificate_key /usr/local/nginx/ssl/ssl.key; // ip访问时https ssl服务端key 存放位置
    }

    server {
        listen 80 default;
        server_name _;                     // 防止ip访问 
        return 500;                        // 返回错误码 500
    }
    server {
        listen       81 default;
        server_name  _;                // 防止ip访问  
        #return 500;
        location /nginx_status {       // 匹配 status 页面 
            stub_status on;            // 开启 status 页面
            access_log off;            // 关闭access_log 即不记录访问日志
            allow 192.168.10.11;       // 允许访问ip段               
            allow 192.168.10.0/24;
            deny all;                  // 拒绝其他所有
         }
    }
    include sites/*.conf;              // 引入下面的文件配置      安装目录下面的sites/下的*.conf文件
}


------------------------------------------------------------------------------------------------------------------------


五. Nginx server 配置文件 详解

upstream ssyy99.com {             // 负载均衡模块 跟名字 只能定义在 http 中 
    ip_hash;                      // 能够将某个ip 的请求定向到同一台后端web机器中,这样一来
                                  // 这个ip下的客户端和某个后端 web机器就能建立起稳固的session
    server 192.168.10.11:80;      // 负载均衡服务器后端地址
    server 192.168.10.12:80;
    server 192.168.10.13:80;
}

server {
    listen 80;
    add_header Strict-Transport-Security max-age=15768000;  // 强制http使用https 单位秒 半年 
    server_name ssyy99.com www.ssyy99.com;                  // 服务器名称
    rewrite ^/(.*) https://$server_name$1 permanent;        // uri重写 permanent  以301响应码(永久重定向)返回新的URL
}       

server {
    listen 443 ssl http2;
    ssl on;                                // 开启ssl 老版本适用  新版本不需要此项目 会报错
    server_name ssyy99.com;
    access_log /usr/local/nginx/logs/ssyy99_access.log main; // 从此域名/端口访问的日志位置
    error_log /usr/local/nginx/logs/ssyy99_error.log;
    ssl_certificate     /usr/local/nginx/ssl/ssyy99.com.crt;       // ssl服务的证书 存放位置
    ssl_certificate_key /usr/local/nginx/ssl/ssyy99.com.key;       // ssl服务端key 存放位置
    location / {                             // 请求的 根  网站路径 匹配的地址
        proxy_redirect off;                  // ***待测试*** 这里为关闭 该指令重写来自于上游服务器的Location 和Refresh 头.
        proxy_set_header        Host            $host;   // 把$host为访问的网站的域名 传递给上端服务器 如没有这项是返回ip 
        proxy_set_header        X-Real-IP       $remote_addr;      //  $remote_addr 记录客户端ip地址
        proxy_set_header        REMOTE_ADDR     $remote_addr;
        proxy_set_header        HTTP_CLIENT_IP  $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for; // 简称XFF头 它代表客户端 HTTP的请求端真实的IP
        client_max_body_size 20m;
        client_body_buffer_size 256k;
        proxy_connect_timeout   300;
        proxy_send_timeout      300;
        proxy_read_timeout      300;
        proxy_pass http://ssyy99.com;                      // 反向代理至 负载均衡 服务器
    }
    location ~ //.(css|js|gif|png|jpg|swf|bmp|swf|flv|ico)$ {   // ~ 正则表达式 // 转意符 以 .*** 结尾的 & 行尾牟定
        proxy_redirect off;
        proxy_set_header        Host            $host;
        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        REMOTE_ADDR     $remote_addr;
        proxy_set_header        HTTP_CLIENT_IP  $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        client_max_body_size 20m;
        client_body_buffer_size 256k;
        proxy_connect_timeout   300;
        proxy_send_timeout      300;
        proxy_read_timeout      300;
        proxy_pass http://ssyy99.com;
    }
    location ~ .*//.(php|php5)?$ {  // ~ 正则表达式 * 代表 前面的任意字符有0个或多个 // 转意符 .php 或.php5 ?一个字符 $行尾牟定
        proxy_redirect off;         // 任意长度任意字符 以 .php[0-9] 或 .php5[0-9]  
        proxy_set_header        Host            $host;
        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        REMOTE_ADDR     $remote_addr;
        proxy_set_header        HTTP_CLIENT_IP  $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        client_max_body_size 20m;
        client_body_buffer_size 256k;
        proxy_connect_timeout   300;
        proxy_send_timeout      300;
        proxy_read_timeout      300;
        proxy_pass http://ssyy99.com;
    }
    location ~ .*//.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
        expires 30d;
    }
    location ~ .*//.(js|css)?$ {
        expires 7d;
    }

    location ~ //.php$ {
        include        fastcgi_params;               // 加载fastcgi文件
        root           /data/www/192.168.10.10/;
        fastcgi_pass   172.17.0.2:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    }                                  // $document_root 为定义的root路径  $fastcgi_script_name指的是index.php
}

注:
    ❶ 开启高效传输模式 提高了发送文件的效率 如果图片显示不正常把这个改成off 可以启用Linux上的sendfile系统调用来发送文件 它减少了
      内核态与用户态之间的两次内存复制 这样就会从磁盘中读取文件后直接在内核态发送到网卡设备 提高了发送文件的效率

    ❷ nginx能处理的最大请求主体大小 默认1m  在接收到请求时 NGINX将其写入这些缓冲区。 这些缓冲区中的数据可作为NGINX变量使用 例如
      $request_body。 如果缓冲区与请求大小相比较小 则数据将写入磁盘上的文件 能够接受客户端最大的请求 大小为50M 如 限制客户端上传
      最大大小 就可以开启这项

    ❸ 该指令指定SSL 缓存以及其是否应该在所有的worker 进程中共享 设置存储session参数的缓存的类型和大小 缓存在所有工作进程之间共享
      SSL起的名字 10m空间 1MB可以存储4000个会话 [Session ID共享复用]

    ❹ 如报错 可删除此项目 提高 TLS 握手效率 启用或禁用 服务器对OCSP响应的装订 启用此项 Web 端将主动获取 OCSP 查询结果并随证书一起
      发送给客户端 以此让客户端跳过自己去寻求验证的过程 提高 TLS 握手效率

    ❺ 浏览器只要向*.com或其子域名发送HTTP请求时 必须采用HTTPS来发起连接 单位为秒为2年有的网站开启了https 但为了照顾用户的使用体验
     (因为用户总是很赖的 一般不会主动键入https 而是直接输入域名, 直接输入域名访问 默认就是http访问)同时也支持http访问 当用户
      http访问的时候 会返回给用户一个302重定向 重定向到https的地址 然后后续的访问都使用https传输,这种通信模式看起来貌似没有问题 
      但细致分析 就会发现种通信模式也存在一个风险 那就是这个302重定向可能会被劫持篡改当用户已经安全的登录开启过htst功能的网站(支持
      hsts功能的站点会在响应头中插入Strict-Transport-Security)之后 支持htst的浏览器(比如chrome. firefox)会自动将这个域名加入到
      HSTS列表 下次即使用户使用http访问这个网站 支持htst功能的浏览器就会自动发送https请求(前提是用户没有清空缓存 如果清空了缓存第
      一次访问还是明文 后续浏览器接收到服务器响应头中的Strict-Transport-Security 会把域名加入到hsts缓存中 然后才会在发送请求前将
      http内部转换成https) 而不是先发送http 然后重定向到https 这样就能避免中途的302重定向URL被篡改。进一步提高通信的安全性。
      add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload"; 在接下来的一年中 如果 
      *.com 服务器发送的TLS证书无效 用户不能忽略浏览器警告继续访问网站

    ❻ ① 当没有配置default server的时候 Nginx将使用找到的第一个配置文件中的配置 
      ② 如果没有指定为 default_server 的话 就跑到 localhost 的节点 如果没有 localhost 的节点 那只好 404 了。 
      ③ 当一个请求无法匹配配置文件中的所有主机域名时 就会选用默认的虚拟主机



------------------------------------------------------------------------------------------------------------------------


六. php 的配置文件 说明

    nginx  通过 fast-cgi 接口 访问php-fpm
    php-fpm 是 php的插件 是 fast-cgi的管理程序  去接待 nginx通过fast-cgi的请求
    php-mysql 是 php的接口 用来 连接 mysql
        php-fpm 核心配置文件 php.ini
            date.timezone =      // 时区
            open_basedir =       // php脚本允许访问的目录
        全局配置文件 php-fpm.conf
            pid = /run/php-fpm/php-fpm.pid             // php-fpm 的pid
            error_log = /var/log/php-fpm/error.log     // php-fpm 的 错误日志
            log_level = notice
            process.max = 3
            daemonize = yes         // 守护进程是否开启 如果开启就不会关闭 常驻内存 除非人为关闭 
        扩展配置文件 conf.d/www.conf
            user = nignx               // 运行php 的 用户
            listen = 127.0.0.1:9000    // 监听地址
            slowlog = /var/log/php-fpm/www-slow.log
            #pm=static                   // 如设置为静态 下面的配置不生效
            pm=dynamic                   // php的进程管理方式 为动态
            pm.start_servers = 5         // 默认启动的进程数
            min_spare_server = 5         // 最小空闲的进程数  永久保存空闲的进程数量
            max_spare_servers = 10       // 最大空闲进程数量 高并发访问过后 还剩下多少空闲进程
            max_children = 50            // 最大进程数 每个占10M-40M 按每个php请求占用内存25M  10G/25M = 409
            max_requests = 500           // 每个子进程能响应的请求数量 到达此数字 php 进程会被释放掉会干掉释放内存 重启新的
            pm.status_path = /php_status // 开启 php-fpm 状态信息页面  同时 nginx 中开启 php-fpm 状态页面
                location = /php_status {
                    fastcgi_pass 127.0.0.1:9000;
                    fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
                    include fastcgi_params;
                }

    # vim php-fpm.conf              // php优化配置文件
        [global]
        pid = /www/server/php/72/var/run/php-fpm.pid
        error_log = /www/server/php/72/var/log/php-fpm.log
        log_level = notice

        [www]
        listen = /tmp/php-cgi-72.sock
        listen.backlog = 8192
        listen.allowed_clients = 127.0.0.1
        listen.owner = www
        listen.group = www
        listen.mode = 0666
        user = www
        group = www
        pm = dynamic
        pm.status_path = /phpfpm_72_status
        pm.max_children = 300
        pm.start_servers = 20
        pm.min_spare_servers = 20
        pm.max_spare_servers = 50
        request_terminate_timeout = 100
        request_slowlog_timeout = 30
        slowlog = var/log/slow.log


    # /www/server/php/70/sbin/php-fpm                                // 启动 php
    # kill -INT 'cat /usr/local/php/var/run/php-fpm.pid'             // 停止 php
    # kill -USR2 `cat /www/server/php/70/var/run/php-fpm.pid`        // 重启 php

........................................................................................................................


七. 附录:


1. nginx 启动脚本
    # vim /etc/init.d/nginx         // 新建启动脚本  注意pid路径是否与主配置文件路径相同...添加启动脚本 nginx
        #!/bin/bash
        # nginx Startup script for the Nginx HTTP Server
        # it is v.0.0.2 version.
        # chkconfig: - 85 15
        # description: Nginx is a high-performance web and proxy server.
        #              It has a lot of features, but it's not for everyone.
        # processname: nginx
        # pidfile: /var/run/nginx.pid
        # config: /usr/local/nginx/conf/nginx.conf
        nginxd=/usr/local/nginx/sbin/nginx
        nginx_config=/usr/local/nginx/conf/nginx.conf
        nginx_pid=/var/run/nginx.pid
        RETVAL=0
        prog="nginx"
        # Source function library.
        . /etc/rc.d/init.d/functions
        # Source networking configuration.
        . /etc/sysconfig/network
        # Check that networking is up.
        [ ${NETWORKING} = "no" ] && exit 0
        [ -x $nginxd ] || exit 0
        # Start nginx daemons functions.
        start() {
        if [ -e $nginx_pid ];then
           echo "nginx already running...."
           exit 1
        fi
           echo -n $"Starting $prog: "
           daemon $nginxd -c ${nginx_config}
           RETVAL=$?
           echo
           [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
           return $RETVAL
        }
        # Stop nginx daemons functions.
        stop() {
                echo -n $"Stopping $prog: "
                killproc $nginxd
                RETVAL=$?
                echo
                [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
        }
        # reload nginx service functions.
        reload() {
            echo -n $"Reloading $prog: "
            #kill -HUP `cat ${nginx_pid}`
            killproc $nginxd -HUP
            RETVAL=$?
            echo
        }
        # See how we were called.
        case "$1" in
        start)
                start
                ;;
        stop)
                stop
                ;;
        reload)
                reload
                ;;
        restart)
                stop
                start
                ;;
        status)
                status $prog
                RETVAL=$?
                ;;
        *)
                echo $"Usage: $prog {start|stop|restart|reload|status|help}"
                exit 1
        esac
        exit $RETVAL


2. Nginx 已优化的配置文件 可以直接使用
    # vim /usr/local/nginx/conf/nginx.conf
        user  www;
        worker_processes  16;

        pid        /var/run/nginx.pid;
        worker_rlimit_nofile 30000;

        events {
            use epoll;
            worker_connections  30000;
        }

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

            log_format main '$server_name $remote_addr $remote_user [$time_local] "$request" '
                            '$status $body_bytes_sent $bytes_sent "$http_referer" '
                            '$request_time $upstream_response_time $pipe '
                            '"$http_user_agent" "$http_x_forwarded_for"';

            server_tokens off;
            sendfile        on;
            client_max_body_size 50m;
            client_body_buffer_size        256k;
            client_header_buffer_size 256k;
            large_client_header_buffers 4 256k;
            
            tcp_nopush on;
            keepalive_timeout  300;
            tcp_nodelay on;

            ssi on;
            ssi_silent_errors on;
            ssi_types text/shtml;

            gzip  on;
            gzip_min_length  1k;
            gzip_buffers     4 16k;
            gzip_http_version 1.0;
            gzip_comp_level 2;
            gzip_types text/plain application/x-javascript text/css application/xml text/javascript;
            gzip_vary on;

            fastcgi_connect_timeout 300;
            fastcgi_send_timeout 300;
            fastcgi_read_timeout 300;
            fastcgi_buffer_size 64k;
            fastcgi_buffers 4 64k;
            fastcgi_busy_buffers_size 128k;

            proxy_buffer_size 256k;
            proxy_buffers 4 256k;
            proxy_busy_buffers_size 256k;

            ssl_session_cache shared:SSL:10m;
            ssl_session_timeout 10m;
            ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
            ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-SHA:AES128-GCM-SHA256:AES128-SHA256:AES128-SHA:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:AES256-GCM-SHA384:AES256-SHA256:AES256-SHA:ECDHE-RSA-AES128-SHA256:!aNULL:!eNULL:!EXPORT:!DES:!3DES:!MD5:!DSS:!PKS;
            ssl_prefer_server_ciphers on;
            ssl_stapling_verify on;
            resolver 8.8.8.8 8.8.4.4 114.114.114.114 223.5.5.5 valid=300s;
            resolver_timeout 10s;
            error_page 497 "https://$host$uri?$args";

            add_header Strict-Transport-Security max-age=63072000;
            add_header X-Content-Type-Options nosniff;

        #   server {
        #        listen 443 default_server ssl;
        #        server_name _;
        #        ssl_certificate /usr/local/nginx/ssl/ssl.crt;
        #        ssl_certificate_key /usr/local/nginx/ssl/ssl.key;
        #   return 500;
        #   }
            
           server
            {
            listen 80 default;
            server_name _;
            return 500;
               }

            server {
                listen       81 default;
                server_name  _;
                #return 500;
            
            location /nginx_status {
                    stub_status on;
                    access_log off;
                allow 192.168.10.13;
                    deny all;
            }

            }
            include sites/*.conf;
        }