FrankKai / FrankKai.github.io

FE blog
https://frankkai.github.io/
363 stars 39 forks source link

nginx入门 #165

Open FrankKai opened 4 years ago

FrankKai commented 4 years ago

https://juejin.im/post/5bd7a6046fb9a05d2c43f8c7 https://docs.nginx.com/nginx/technical-specs/

FrankKai commented 4 years ago

nginx基础

查看nginx版本

nginx -v

查看centOS上所有nginx相关内容

rpm -ql nginx

查看nginx支持的mime types

/etc/nginx/mime.types//一开始就是前端三板斧的text/html, text/css, application/javascript

nginx.pid是什么

是nginx运行时,控制它的一个进程。nginx -s stop后,会删除这个文件。默认保存在/run/nginx.pid

nginx配置虚拟主机

利用虚拟主机把多个不同域名的网站部署在同一台服务器上。 可以基于端口号,基于ip,基于域名配置。

nginx的conf.d是什么目录?

在nginx.conf文件,会通过include /etc/nginx/conf.d/*.conf;导入配置文件。

https://www.linode.com/docs/web-servers/nginx/how-to-configure-nginx/#server-blocks

nginx杂

upstream是什么?

Proxy and cache requests to load-balanced pool of servers(proxy请求和cache请求服务器负载均衡池)。 用来做服务器负载均衡。

proxy_pass是什么?

Sets the address of a proxied server. The address can be specified as a domain name or IP address, and a port。 设置代理服务器的地址。 这个是反向代理需要配置的字段。

server_name为_是什么意思?

http://nginx.org/en/docs/http/server_names.html Server Name may be defined using exact names, wildcard names, or regular expressions.

server {
    listen       80;
    server_name  example.org  www.example.org;
    ...
}
server {
    listen       80;
    server_name  *.example.org;
    ...
}

_是什么意思呢?

这个名字没有什么特别之处,它只是无数无效域名中的一个,这些域名与任何真实的名字都没有交集。其他无效的名称,如“——”和“!”@#”同样可以使用。

FrankKai commented 4 years ago

nginx常用

查看nginx配置

/etc/nginx/nginx.conf

启动nginx

nginx

查看nginx服务是否正常

ps -ef | grep nginx // 可以看到nginx进程包括master和worker两个进程

nginx服务相关

nginx -s stop stop SIGTERM暴力中止 quit SIGQUIT等待进程完成工作再中止 reopen SIGUSR1重启 reload SIGHUP重新加载配置

nginx 白名单、黑名单

location / {deny 61.164.40.194; allow 61.164.40.194;}

nginx只允许某台机器访问

location / { allow 61.164.40.194; deny all;}权限指令先出现的覆盖后出现的

nginx配置目录权限

location =/img{allow all;} location =/admin {allow 192.168.0.111;deny all;}// img目录允许所有用户访问;admin目录只允许内网固定ip访问,外网禁止。

nginx html文件目录

/usr/share/nginx/html这个是server的root目录,存放了404.html, 50x.html, index.html, nginx-logo.png,poweredby.png文件。

FrankKai commented 4 years ago

nginx实践

为什么在conf.d目录下新建default.conf无法生效?

安全组需要设置放通web服务端口0.0.0.0/0 | TCP:80,443,8001,8002 | 允许 | 放通Web服务端口

8001是在/etc/nginx/conf.d/default.conf下配置的。

server{
        listen 8001;
        server_name localhost;
        root /usr/share/nginx/html/html8001;
        index index.html;
}
// NODE NAME是*:vcom-tunnel (LISTEN)

8002直接在/etc/nginx/nginx.conf配置。

  server {
      listen 8002;
      server_name localhost;
      root /usr/share/nginx/html;
      index index.html;
   }
// NODE NAME是*:teradataordbms (LISTEN)
[root@VM_0_11_centos conf.d]# lsof -i :80
COMMAND   PID  USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
nginx   11463  root    7u  IPv4 3764189      0t0  TCP *:http (LISTEN)
nginx   11463  root    8u  IPv6 3764190      0t0  TCP *:http (LISTEN)
[root@VM_0_11_centos conf.d]# lsof -i :8001
COMMAND   PID  USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
nginx   11463  root   11u  IPv4 3778876      0t0  TCP *:vcom-tunnel (LISTEN)
nginx   17591 nginx   11u  IPv4 3778876      0t0  TCP *:vcom-tunnel (LISTEN)
[root@VM_0_11_centos conf.d]# lsof -i :8002
COMMAND   PID  USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
nginx   11463  root   13u  IPv4 3781031      0t0  TCP *:teradataordbms (LISTEN)
nginx   17591 nginx   13u  IPv4 3781031      0t0  TCP *:teradataordbms (LISTEN)

nginx的域名类型虚拟主机与公网解析的有什么区别?

nginx域名虚拟主机,可以用来搭建内网的开发环境,配置多个域名到一台机器的不同端口,但是需要通过带端口的方式去访问。例如foo.com 80,bar.com 8081,baz.com 8082,访问foo.com可以直接访问,但是访问bar.com和baz.com时,就需要bar.com:8081和baz.com:8082了。这个需要修改本地的/etc/hosts文件,三个域名指向同一台内网机器的ip。

nginx配置公网解析,没差,只是解析到公网,可以指定端口。注意这里也需要改本地hosts。

// 远程服务器nginx配置
server {
      listen 8002;
      server_name foo.cn;
      root /usr/share/nginx/html;
}
// 本地host配置
122.51.44.78 foo.cn

访问方式:配置了host的机器通过http://foo.cn:8082 访问即可。

内网开发有什么办法不带端口吗?

proxy。也就是反向代理。 也就是说,nginx只监听80端口,然后对80端口不同域名做区分,当对应server_name来访问时,会被proxy到相应的目录或者机器。

访问crm.test.weidiango.com,会被代理到http://crm.test.weidiango.com/。 访问fe.crm.test.weidiango.com前端地址,会去本机的/root/www/webroot/dist目录找index.html。 访问index.test.weidiango.com,会被代理到http://index.test.weidiango.com/。

server {
                listen  80;
                server_name crm.test.weidiango.com;
                location / {
                        proxy_pass      http://crm.test.weidiango.com/;
                }
 }
server {
        listen 80;
        server_name fe.crm.test.weidiango.com;
        location / {
            root /root/www/webroot/dist;
            index index.html;
        }
}
server {
              listen       80;
              server_name  index.test.weidiango.com;
              location / {            
            proxy_pass   http://index.test.weidiango.com/;
            client_max_body_size    100m;
            }
}

访问crm.test.weidiango.com,会被代理到http://crm.test.weidiango.com/。 访问index.test.weidiango.com,会被代理到http://index.test.weidiango.com/。 上面这两个不是很懂,代理不代理有啥区别?也没有指向别的机器啊?

http://crm.test.weidiango.com/http://index.test.weidiango.com/ ,是两个公网ip,所以在这里的配置是完全没有必要的。

// 现在的配置
192.168.0.111 crm.test.weidiango.com // 本地host,连预生产时。
crm.test.weidiango.com -> http://crm.test.weidiango.com // 代理。
192.168.0.146 crm.test.weidiango.com // 本地host,连服务端某个开发本地时
crm.test.weidiango.com:9090// 前端接口访问配置,连服务端某个开发本地时
// 更好的配置
# 192.168.0.111 crm.test.weidiango.com // 本地host,连预生产时。**注释掉**
crm.test.weidiango.com -> http://crm.test.weidiango.com // 代理。**根本不需要,可删除**
192.168.0.146 crm.test.weidiango.com // 本地host,连服务端某个开发本地时
crm.test.weidiango.com:9090// 前端接口访问配置,连服务端某个开发本地时

192.168.0.111,fe.crm.test.com(前端),192.168.0.111 crm.test.com(服务端),192.168.0.111 index.test.com(其他系统)。这些域名都指向一台内网服务器111的nginx,通过静态指向或者代理配置域名虚拟主机,达到一台机器为前端静态文件,后端服务,其他系统后端服务提供支持的效果。

如果说proxy到的是另一台内网的机器,例如proxy_pass: 192.168.0.67,这样的proxy_pass才是符合逻辑的。

但是通过一台固定的机器去做反向代理,统一管理接口访问,也是值得推荐的。

反向代理可以保护服务器,所有的访问都是走proxy,从而并不知道具体访问了哪台机器,做到了保护服务器的效果;反向代理还可以做负载均衡和缓存等等,服务器压力平均分配。

最简单的反向代理

// 122.51.44.78服务器配置
server {
      listen 8002;
      server_name foo.cn;
      location / {
          proxy_pass http://mainSystem.cn/;
}
// 本地host映射
122.51.44.78 foo.cn

访问方式:foo.cn:8002,效果是这个请求最终会落到http://mainSystem.cn。 假如访问服务端接口,foo.cn:8002/foo/bar,最终会落到http://mainSystem.cn/foo/bar。

如果有其他系统服务需要访问,增加一个配置即可。

// 122.51.44.78服务器配置
server {
      listen 8002;
      server_name bar.cn;
      location / {
          proxy_pass http://otherSystem.cn/;
}
// 本地host映射
122.51.44.78 bar.cn

假如访问服务端接口,bar.cn:8002/foo/bar,最终会落到http://otherSystem.cn/foo/bar。

做到了反向代理的效果。

foo.cn:8002/foo/bar -> http://mainSystem.cn/foo/bar
bar.cn:8002/foo/bar -> http://otherSystem.cn/foo/bar

反向代理可以通过proxy_set_header,proxy_connect_timeout等等,对请求和响应做到控制。(传递消息的中间人,就是可以篡改消息,就是这么强。)

nginx如何区分pc和mobile?

除了bootstrap,media query和rem等自适应方式,pc端和移动端分开编写的话,才更适合复杂的业务逻辑。

cd /usr/share/nginx/html/html8001/
mkdir pc
touch index.html
vim index.html // <h1>PC</h1>
cd /usr/share/nginx/html/html8001/
mkdir mobile
touch index.html
vim index.html // <h1>Mobile</h1>
server{
        listen 8001;
        server_name localhost;
        location / {
           root /usr/share/nginx/html/html8001/pc;
           if ($http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry)') {
               root /usr/share/nginx/html/html8001/mobile;
           }
           index index.html;
        }
}

通过http://122.51.44.78:8001/ 在电脑和手机上访问,会返回pcmobile两种不同的消息。

如何开启gzip压缩?

http    {
    gzip                on;
    gzip_types     text/plain application/javascript text/css; 
}

开启gzip压缩之后,响应头的Content-Encoding:gzip。请求头的Accept-Encoding:gzip与nginx配置无关,chrome默认是带这个请求头的。