nginx(Engine X) 高性能的web服务器
排名指路:https://w3techs.com/technologies/cross/web_server/ranking
官网指路:http://nginx.org/en/

轻量级: CPU,内存占用少(工作模式:进程池 + 单线程 => 消除了进程,线程切换的成本)
One master and several worker processes; worker processes run under an unprivileged user;
(在进程之上,master进程用来管理进程池,监控进程,自动恢复发生异常的worker,保持进程池的稳定和服务能力)
cite: https://time.geekbang.org/column/article/117492

nginx 怎么找

  1. ps -ef | grep nginx (ps:显示系统进程)
  2. whereis nginx 软件安装路径(whereis – locate programs;The whereis utility checks the standard binary directories for the specified programs, printing out the paths of any it finds.The path searched is the string returned by the sysctl(8) utility for the ``user.cs_path’’ string)
  3. which nginx 运行文件所在路径(which – locate a program file in the user’s path;The which utility takes a list of command names and searches the path for each executable file that would be run had these commands actually been invoked.)
  4. cd / && find - name ‘nginx.conf’
    nginx.conf => 基本的
    vhost => 应用的

nginx 403

1
2
chmod -R 777
ps aux | grep "nginx: worker process" | awk '{print $1}'
nginx 
root

vim nginx.conf把 user 改成 root,即可;

firekylin nginx

1
ln -s /root/firekylin/nginx.conf /etc/nginx/conf.d/firekylin.conf

nginx.conf里面:

~   #波浪线表示执行一个正则匹配,区分大小写

# location ~ /static/ {
# etag  on;
# expires  max;
# } 

# etag  on;
# expires  max;

都注释了,还是403

location ~ /static/ {}
也注释了,就生效了

文档指路:http://nginx.org/en/docs/beginners_guide.html

kill nginx 服务

nohup = No HangUP

1
2
3
4
5
6
7
8
9
10
11
12
ps -ef|grep nginx
cd /usr/local/nginx/sbin
ls
./nginx -t
./nginx -s reload

kill -QUIT 28761
nohup /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
tail -2000f nohup.out
ps -ef|grep nginx
# 日志
# reload无效的时候

前端history路由模式

1
2
3
location / {
try_files $uri $uri/ /index.html;
}

正则匹配

http server优先级

线上出过一个问题,之前一直没生效的CXP的配置生效了;原因是server优先级高于http,修改了server里面的一些跨域的配置;

应用场景

本地起nginx代理解决资源跨域或者连本地服务调试(嵌入移动端的页面)问题

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
nginx -v # 查看本机是否安装nginx, 如果没有安装homebrew安装下;
brew info nginx # 可获得nginx文件的配置位置
cd /usr/local/etc/nginx
code nginx.conf # 打开nginx配置文件
# 增加nginx server 80 下面的配置,这样test.company.cn就会访问到本地我们起的http://127.0.0.1:8886服务上
http {
include mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_body_buffer_size 8m;
server_tokens off;
ignore_invalid_headers on;
recursive_error_pages on;
server_name_in_redirect off;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
#keepalive_timeout 0;
keepalive_timeout 65;
server {
listen 80;
server_name test.company.cn;

location / {
proxy_pass http://127.0.0.1:8886;
}
}
}
# 修改系统hosts:
127.0.0.1 test.company.cn
127.0.0.1 localhost

# 这样,在浏览器里面输入test.company.cn就可以访问我们本地起的服务 http://127.0.0.1:8886 了;