Rootop 服务器运维与web架构

2021-09-15
发表者 Venus
nginx反向代理proxy_pass中带/和不带/的区别已关闭评论

nginx反向代理proxy_pass中带/和不带/的区别

文档:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

环境:
192.168.31.36 nginx反向代理
192.168.31.37 nginx,默认首页为index.html

通过浏览器访问:http://www.test.com/test/index.html 测试

情况一、
proxy_pass中不包含URI地址,那么location后的地址会传递给后端。
1、

location /test
{
	proxy_pass http://192.168.31.37;
}

因为后端没有test目录,所以访问结果为 404(后端日志 “GET /test/index.html HTTP/1.0” 404)

2、

location /test/   # location中以/结尾
{
	proxy_pass http://192.168.31.37;
}

因为后端没有test目录,所以访问结果为 404(后端日志 “GET /test/index.html HTTP/1.0” 404)

情况二、
proxy_pass中指定了符合URI规范的地址,那么location后面匹配的地址字符被替换为proxy_pass中URI的地址字符
3、

location /test
{
    proxy_pass http://192.168.31.37/;
}

proxy_pass中的/替换为/test,实际访问 http://192.168.31.37//index.html,所以访问成功(后端日志 “GET //index.html HTTP/1.0” 200)
实际访问地址里出现//的原因是访问
/test/index.html 时,proxy_pass后面的/替换为/test,变成
//index.html
所以日志里也会看到两个//。

4、

location /test/
{
	proxy_pass http://192.168.31.37/;
}

访问成功(后端日志 “GET /index.html HTTP/1.0” 200)
/ 替换 /test/ 后就变为 /index.html ,所以日志里只出现一个 /

2021-09-01
发表者 Venus
请求头参数带下划线,后端无法接收(underscores_in_headers)已关闭评论

请求头参数带下划线,后端无法接收(underscores_in_headers)

如果在请求头中有下划线的参数时,nginx则会默认屏蔽此头信息。


比如 access_token 之类的想用来做身份认证时,就不能用下划线,要用中线 – 替代。

可以在server{}段中添加参数,允许下划线,但是不推荐:

underscores_in_headers on;

这样nginx就会接受带下划线的请求头(nginx默认配置是off)。

提及此原因的说明在此:
https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/?highlight=underscores#missing-disappearing-http-headers
Missing (disappearing) HTTP Headers
If you do not explicitly set underscores_in_headers on;, NGINX will silently drop HTTP headers with underscores (which are perfectly valid according to the HTTP standard).
This is done in order to prevent ambiguities when mapping headers to CGI variables as both dashes and underscores are mapped to underscores during that process.

2021-09-01
发表者 Venus
使用ipip地址库屏蔽城市已关闭评论

使用ipip地址库屏蔽城市

ipdb和geoip2模块非常相似,前者为国内公司做的ip地址库,后者为国外搞的。不过免费版都不是非常精准。
模块地址:https://github.com/vislee/ngx_http_ipdb_module

[root@MiWiFi-RA69-srv ~]# yum install json-c json-c-devel
nginx编译时加上参数:
--add-module=/home/ngx_http_ipdb_module-master

去ipip.net下载免费版的地址库文件(https://www.ipip.net/product/ip.html#ipv4city)
最后更新时间是2019年7月,版本还是挺老了。

以下部分在http{}段配置:

ipdb /home/www/ipipfree.ipdb;
ipdb_language CN;

# 定义map
map $ipdb_city_name $allowed_city {
default yes;
"北京" no;
"上海" no;
}

以下部分在server{}段配置:

server
{
    listen 80;
    server_name www.test.com;
    index index.html;
    root /home/www/www.test.com;

	location /
	{	
		if ($allowed_city = "no")
		{
		  return 403;
		}
	}
	
	# 提供一个查询接口
	location /ip
	{
		# ipdb_specifies_addr $http_addr;
		# ipdb_language EN;
		default_type application/html;
		add_header Content-Type "text/plain;charset=utf-8";
		return 200 "country_name:$ipdb_country_name, city_name:$ipdb_city_name,raw_info:$ipdb_raw";
	}
		
    access_log  off;
    error_log  /dev/null;
}

关于模块中更多的内置变量,可以去看github中文档。

2021-08-30
发表者 Venus
linux suid sgid已关闭评论

linux suid sgid

suid、sgid实现的是让普通用户、或者属组提升权限为这个文件的所有者。
# 创建一个空文件
[root@rootop ~]# touch a
[root@rootop ~]# ll
total 4071212
-rw-r–r– 1 root root 0 Jul 23 15:49 a

# 设置SetUID
[root@rootop ~]# chmod 4777 a
[root@rootop ~]# ll
total 4071212
-rwsrwxrwx 1 root root 0 Jul 23 15:49 a

# 设置SetGID
[root@rootop ~]# chmod 2777 a
[root@rootop ~]# ll
total 4071216
-rwxrwsrwx 1 root root 0 Jul 23 15:49 a

# 设置Stick Bit,粘滞位。
[root@rootop ~]# chmod 1777 a
[root@rootop ~]# ll
total 4071216
-rwxrwxrwt 1 root root 0 Jul 23 15:49 a

# 等同于chmod u+s a 实现suid添加
# 等同于chmod g+s a 实现sgid添加
# 等同于chmod o+t a 实现stick bit添加

有时在权限位会出现大S和小s(或者是大T和小t)情况,这是由于用户、组、其它,没有执行(x)权限,添加特殊权限的时候就会变成大写S(或者大写T)。
[root@rootop ~]# chmod u+s b
[root@rootop ~]# ll
total 0
-rwSr–r– 1 root root 0 Aug 30 13:25 b

[root@rootop ~]# touch c
[root@rootop ~]# chmod u+s c
[root@rootop ~]# chmod o+t c
[root@rootop ~]# ll
-rwSr–r-T 1 root root 0 Aug 30 13:25 c

2021-08-27
发表者 Venus
自动安装anaconda(conda)已关闭评论

自动安装anaconda(conda)

想实现自动安装anaconda(conda)python多版本环境,就用了expect实现自动应答。
脚本如下:

#!/usr/bin/expect
set timeout -1
spawn sh Anaconda2-2019.10-Linux-x86_64.sh
expect "press ENTER to continue"
send "\r"

expect "from Microsoft Corporation"
send "q"

expect "Do you accept the license terms"
send "yes\r"

expect "/root/anaconda2"
send  "/usr/local/anaconda2\r"

expect "by running conda init"
send "no\r"

expect eof

[root@MiWiFi-RA69-srv ~]# chmod 777 conda.sh
[root@MiWiFi-RA69-srv ~]# ./conda

测试通过。

最后发现anaconda支持静默安装。。。

# Anaconda 静默安装
官文:https://conda.io/projects/conda/en/latest/user-guide/install/macos.html#install-macos-silent
-b: Batch mode with no PATH modifications to shell scripts. Assumes that you agree to the license agreement. Does not edit shell scripts such as , , , etc..bashrc.bash_profile.zshrc

-p: Installation prefix/path.

-f: Force installation even if prefix already exists.-p

# 静默安装方法

[root@MiWiFi-RA69-srv ~]# sh Anaconda2-2019.10-Linux-x86_64.sh -b -p /usr/local/anaconda2/

conda版本下载归档:
https://repo.anaconda.com/archive/