It's ALWAYS DNS!

今天前端遇到一个问题,前端部署的[反向代理]](/posts/nginx-proxy/)到后端的upstream一直pending。 timeout? 以为是后端服务压力大,来不及响应,所以更新upstream配置,加上timeout。 立竿见影,没问题了。 1 2 3 4 5 6 7 8 9 10 11 12 location /api/ { proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; rewrite ^/api/(.*)$ /$1 break; proxy_pass http://api_server/; proxy_connect_timeout 5s; proxy_send_timeout 10s; proxy_read_timeout 10s; } 一段时间后,后端又接受不到前端请求了,nginx一直报错499。 DNS ! 调查了一段时间后发现根本问题是nginx的dns cache机制的问题。 原来后端每次更新k8s后端deployment的时候,也会重建service从而导致 service的 IP发生了变化。 和后端沟通修改了后端更新流水线脚本,不再重建service 后,问题解决 :) dubug force resolve (bad performance) force nginx to resolve DNS (of a dynamic hostname) everytime when doing proxy_pass? 1 2 3 4 5 6 7 server { #... resolver 127.0.0.1; set $backend "http://dynamic.example.com:80"; proxy_pass $backend; #... } resolver ...

May 19, 2022 · datewu

配置http selinux

今天在digitocean一台新申请的主机上部署web应用。部署完成,打开浏览器发现报错403。 部署的web应用很简单,后端用nginx做了反向代理,应该没啥大问题。 进一步打开chrome的console,发现对static file的访问报错403,还没到后端就已经报错了, 估计后面的 upstream socket也会报错。 ssh登陆到服务器上看了下nginx的日志,发现是权限的问题。 进一步debug了之后发现虚拟机开启selinux,当时心头就一紧,估计要改selinux配置了,这是个麻烦事儿。 想简单点直接关闭selinux,转念一想digitocean的主机直接暴露在interner上,开着selinux 其实是个很好的保护。digitocean打开自有它打开的道理,我猜可能有很多vm被攻破沦为僵尸网络了。 google搜索了selinux的web server常用的配置,验证后,解决了nginx 403的问题。记录分享如下: check seLinux 查看系统状态 查看selinux配置和状态 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 [root@deo ~]# sestatus -v SELinux status: enabled SELinuxfs mount: /sys/fs/selinux SELinux root directory: /etc/selinux Loaded policy name: targeted Current mode: enforcing Mode from config file: enforcing Policy MLS status: enabled Policy deny_unknown status: allowed Max kernel policy version: 28 Process contexts: Current context: unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 Init context: system_u:system_r:init_t:s0 /usr/sbin/sshd system_u:system_r:sshd_t:s0-s0:c0.c1023 File contexts: Controlling terminal: unconfined_u:object_r:user_devpts_t:s0 /etc/passwd system_u:object_r:passwd_file_t:s0 /etc/shadow system_u:object_r:shadow_t:s0 /bin/bash system_u:object_r:shell_exec_t:s0 /bin/login system_u:object_r:login_exec_t:s0 /bin/sh system_u:object_r:bin_t:s0 -> system_u:object_r:shell_exec_t:s0 /sbin/agetty system_u:object_r:getty_exec_t:s0 /sbin/init system_u:object_r:bin_t:s0 -> system_u:object_r:init_exec_t:s0 /usr/sbin/sshd system_u:object_r:sshd_exec_t:s0 查看文件目录的selinux label 1 2 3 4 5 6 [root@deo ]# ls -Z /opt/todolist -rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 index.html drwxr-xr-x. root root unconfined_u:object_r:admin_home_t:s0 static [root@deo ]# ls -Z /usr/share/nginx/html -rw-r--r--. root root system_u:object_r:httpd_sys_content_t:s0 50x.html -rw-r--r--. root root system_u:object_r:httpd_sys_content_t:s0 index.html meat 下面两种方法选一种就可以了 ...

April 16, 2018 · datewu

部署moodle

客户需要部署一套 moodle 教学系统。 去moodle官网大致看了一圈,发现moodle 是一个典型的PHP web应用。 其实这种LAMP (Linux, Apache, MySQL, PHP/Perl/Python)的应用, 我一般会用docker componse快速部署的,比如这个docker componse看上去就很不错。 但是客户不想用docker,要求直接在vm上部署。 初步确认部署环境为: nginx(let's encrypt) + php 7.2 + pg 10 + Centos 7.4 。 安装软件 初始化主机 1 2 3 4 5 6 7 8 9 10 hostnamectl set-hostname deoops.com # disable passwd login; use ssh-key only vi /etc/ssh/sshd_config yum update -y yum upgrade -y init 6 # add remi repo rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm 安装nginx 1 2 3 4 5 6 7 8 9 10 11 yum install nginx yum -y install yum-utils yum-config-manager --enable rhui-REGION-rhel-server-extras rhui-REGION-rhel-server-optional ## 安装let's encrypt certbot yum install certbot-nginx systemctl enable nginx systemctl start nginx ## 签发证书 certbot --nginx certonly ls -alh /etc/nginx/ 安装php dependency 1 yum --enablerepo=remi,remi-php72 install php-fpm php-common php-opcache php-pecl-apcu php-cli php-pear php-pdo php-mysqlnd php-pgsql php-pecl-mongodb php-pecl-redis php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml 配置nginx + php-fpm 详细的配置内容看这里 ...

March 20, 2018 · datewu

orange网关

几天之前试用过了kong效果不理想 ,今天来使用下小米出品(存疑?)的 orange网关。 一个明显的区别是 kong的后端存储使用了postgresql,orange使用的是mysql。 好了,废话不多说,贴出安装部署的过程如下: 安装 mysql 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #!/bin/bash wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm sudo yum update sudo yum install mysql-server sudo systemctl start mysqld sudo systemctl enable mysqld sudo mysql_secure_installation vi save_your_root_pwd git clone https://github.com/sumory/orange.git cd orange/ ls cd install/ ls head orange-v0.6.4.sql head -n 100 orange-v0.6.4.sql head -n 50 orange-v0.6.4.sql mysql -V mysql -u root -p cd orange/ cd install/ ls mysql -u o_usr -p o_database < orange-v0.6.4.sql mysql -u o_usr -p o_database orange 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #!/bin/bash yum remove kong-community-edition ## get rid of annoying lua 5.1 version conflict cd /data/nginx/conf/ cp api.conf api.conf.bak.18.03.09 ## always backup conf files : ) nginx -s stop netstat -nlp | grep 443 mv /usr/sbin/nginx /usr/sbin/nginx_old yum install yum-utils yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo yum install openresty openresty-resty -y git clone https://github.com/sumory/orange.git git clone https://github.com/sumory/lor.git pwd mv lor ~ mv orange ~ cd ls cd lor/ make install cd ../orange/ make install cd ln -s /usr/local/bin/orange /bin/orange ln -s /usr/local/openresty/nginx/sbin/nginx /bin/nginx mv /home/deoops/orange.conf /home/deoops/nginx.conf /usr/local/orange/conf/ vi /usr/local/orange/conf/orange.conf ## make sure orange.conf has the right mysql server info vi /data/nginx/conf/api.conf chown root:root /usr/local/orange/conf/*.conf orange start netstat -nlp | grep 443 小结 用了几天,感觉UI比起kong来说简单些,开箱即用的功能比kong也多一些。 稳定性还有待进一步的观察。 ...

February 5, 2018 · datewu

kong网关

updated: kong不满足要求,后面调研了另外一个API产品 orange 2015年接触openresty的时候接触过kong,了解到kong是基于openresty二次开发的商业产品, 正好目前新公司要调研稳定好用的API Gateway产品,所以本文简单记录下我对kong的安装配置感受。 安装 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 yum install https://bintray.com/kong/kong-community-edition-rpm/download_file?file_path=centos/7/kong-community-edition-0.12.1.el7.noarch.rpm kong systemctl stop nginx netstat -nlp netstat -nlp|grep 80 ls date vi /etc/kong/kong.conf.default vipw su - postgres create user kong; create database kong ownner kong; create database kong owner kong; cd /etc/kong/ cp kong.conf.default kong.conf vi kong.conf kong migrations up psql -U kong tail /media/data/pgdata/log/postgresql-Wed.log vi /media/data/pgdata/pg_hba.conf systemctl restart postgresql-10.service kong migrations up kong kong check kong start netstat -nlp | grep 80 which nginx curl localhost: netstat -nlp vi /usr/local/kong/nginx-kong.conf which -a nginx kong kong restart --nginx-conf /etc/nginx/nginx.conf cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.tmpl vi /etc/nginx/nginx.conf.tmpl kong restart --nginx-conf /etc/nginx/nginx.conf.tmpl which -a npm 安装控制台 使用 ...

February 3, 2018 · datewu

代理和反向代理

代理 (正向)代理 代理一般是指正向代理,比如翻墙软件shadowsocks就是一种正向代理。 shadowsocks通过socks 5协议 在代理服务器上, 代理我们(client)去访问被墙的资源(google/twitter/Facebook等服务器)。 A proxy server, sometimes referred to as a forward proxy, is a server that routes traffic between client(s) and another system, usually external to the network. By doing so, it can regulate traffic according to preset policies, convert and mask client IP addresses, enforce security protocols, and block unknown traffic. 反向代理 反向代理是说我们(client)被代理了, 我们自己还不知道。 我们以为和我们打交道的(处理我们的请求)的是nginx 服务器,其实nginx真正处理我们请求的是ngix后面的upstream在 处理我们的请求逻辑。 ...

May 19, 2017 · datewu

缓存静态文件

众所周知nginx有很强的分发静态文件的能力,很多时候nginx对静态资源分发能力的瓶颈和redis一样在主机的网卡上。 (一般虚拟机的网卡只有500mbps,如果你使用的是万兆的物理网卡就当我没说) 和redis对比,nginx有另外一个瓶颈在服务器的硬盘IO上,SSD硬盘情况会好一些, 所以很多情况下,我们会把 nginx的cache 做在系统的ssd硬盘上, 其实还可以直接把cache放到内存文件系统里,进一步提升磁盘io吞吐。 tmpfs differences between ramfs and tmpfs 1 2 3 4 5 #!/bin/bash mkdir /mnt/ramdisk mount -t tmpfs -o size=512m tmpfs /mnt/ramdisk echo 'tmpfs /mnt/ramdisk tmpfs nodev,nosuid,noexec,nodiratime,size=1024M 0 0' >> /etc/fstab nginx http cache config 1 2 3 4 5 6 http { more_set_headers 'Server: CachedLOL'; proxy_cache_path /var/cache/nginx levels=1:2 use_temp_path=on keys_zone=one:500m max_size=5g inactive=120m; proxy_temp_path /var/cache/nginx/tmp 1 2; } location conf 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 upstream upV1 { server 172.26.2.5:9090 fail_timeout=0; server 172.26.2.6:9090 fail_timeout=0; } server { listen 80 default backlog=16384; server_name tab.deoops.com; location ~* ( /static.*|/list.+|/ )$ { proxy_redirect off; proxy_cache one; proxy_ignore_headers "Set-Cookie"; proxy_hide_header "Set-Cookie"; add_header X-Cache $upstream_cache_status; proxy_cache_key $uri$is_args$args$mobile; proxy_cache_min_uses 1; proxy_cache_valid 120m; proxy_cache_use_stale error timeout; proxy_buffering on; proxy_pass http://upV1; }

April 29, 2017 · datewu