Nginx反向代理 + Apache2 + PHP + Mysql 组合下的全站点HTTPS

1)wp-config.php中增加以下内容

$_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_HOST'];
$_SERVER['HTTPS'] = 'on';

define('WP_HOME', 'https://'.$_SERVER['HTTP_HOST']);
define('WP_SITEURL', 'https://'.$_SERVER['HTTP_HOST']);

define('FORCE_SSL_LOGIN', true);
define('FORCE_SSL_ADMIN', true);

2)nginx 配置

server {
    listen       80;
    server_name  daijiyu.com www.daijiyu.com;
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl;
    server_name www.daijiyu.com;
    ssl_certificate /etc/nginx/ca/1_www.daijiyu.com_bundle.crt;
    ssl_certificate_key /etc/nginx/ca/2_www.daijiyu.com.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;
    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        proxy_pass http://127.0.0.1:6011;
    }
}

HTTP状态码整理

结合我自己日常时间所遇到的一些情况和理解,来整理一下HTTP状态码

1xx 临时相应

100 continue (继续)

这个状态码实际上是对如下场景的一种优化:客户端有一个较大的文件需要上传并保存,但是客户端不知道服务器是否愿意接受这个文件,所以希望在消耗网络资源进行传输之前,先询问一下服务器的意愿。

继续阅读“HTTP状态码整理”

Consul+upsync+Nginx 动态负载均衡 方案

Consul服务器

下载consul_1.6.2_linux_amd64.zip,解压至/usr/bin,依赖unzip

mkdir -p /data/consul
mkdir /etc/consul.d
consul agent -server -bootstrap-expect=1 -data-dir=/data/consul -node=agent-one -bind=192.168.1.95 -enable-script-checks=true -config-dir=/etc/consul.d -client 0.0.0.0 -ui

nohup启动方式

mkdir /var/opt/consul
nohup consul agent -server -bootstrap-expect=1 -data-dir=/data/consul -node=agent-one -bind=192.168.1.95 -enable-script-checks=true -config-dir=/etc/consul.d -client 0.0.0.0 -ui >> /var/opt/consul/consul.log 2>&1 &

Nginx服务器

前置依赖

apt-get install -y unzip gcc make

nginx 配置用户和组

groupadd nginx
useradd -g nginx -s /sbin/nologin nginx
mkdir -p /var/tmp/nginx/client/
mkdir -p /usr/local/nginx

ubuntu编译安装Nginx

apt-get install -y openssl libssl-dev libpcre3 libpcre3-dev zlib1g-dev
./configure   --prefix=/usr/local/nginx   --user=nginx   --group=nginx   --with-http_ssl_module   --with-http_flv_module   --with-http_stub_status_module   --with-http_gzip_static_module   --with-http_realip_module   --http-client-body-temp-path=/var/tmp/nginx/client/   --http-proxy-temp-path=/var/tmp/nginx/proxy/   --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/   --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi   --http-scgi-temp-path=/var/tmp/nginx/scgi   --with-pcre --add-module=../nginx-upsync-module-master
make && make install

配置upsync

mkdir /usr/local/nginx/conf/servers/

upsync_dump_path指定从consul拉取的上游服务器后持久化到的位置,这样即使consul服务器出问题了,本地还有一个备份。

POSTMAN发包创建upstream

   upstream 98apache2{
        server 127.0.0.1:11111;
        upsync 192.168.1.95:8500/v1/kv/upstreams/98apache2 upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;
        upsync_dump_path /usr/local/nginx/conf/servers/98apache2.conf;
    }

    server {
        listen       80;
        server_name  localhost;

        location / {
            proxy_pass http://98apache2;
            index  index.html index.htm;
        }
    }

kv中可配置权重

{"weight":2, "max_fails":2, "fail_timeout":10, "down":0}

node.js 安装 npm 并管理 npm 版本

更新npm到最新版本

npm install npm@latest -g.

查看当前npm版本

npm -v

在卸载gitlab然后再次安装执行sudo gitlab-ctl reconfigure的时候往往会出现:ruby_block[supervise_redis_sleep] action run,会一直卡无法往下进行!

解决方案:

1、按住CTRL+C强制结束;
2、运行:sudo systemctl restart gitlab-runsvdir;
3、再次执行:sudo gitlab-ctl reconfigure

nginx配置动态域名解析

resolver 114.114.114.114;
 
server {
    location / {
        set $servers github.com;
        proxy_pass http://$servers;
    }
}

Nginx对其中配置的代理域名,仅在程序启动的时候解析一次,把IP缓存下来,导致IP变化时,出现502 Bad Gateway的报错。此写法可以指定解析时使用的DNS,但是当IP动态变化之后,依然无法正常解析,故实际测试之后,我使用的是这个方法:

增加一条crontab,定时重载NGINX,重新获取IP地址

*/10 *  * * * /usr/sbin/nginx -s reload