msg 2019-03-22
最近我们的angular项目部署,我们采用的的是Nginx,下面对Nginx做一个简单的介绍。
PDF幻灯片可以到安科网资源站下载:
------------------------------------------分割线------------------------------------------
具体下载目录在 /2019年资料/3月/22日/Nginx安装部署之反向代理配置与负载均衡/
------------------------------------------分割线------------------------------------------
虽然一般的服务器都不使用windows系统,我们还是先来一段window的

nginx -s stop #停止nginx nginx -s reload #重新加载nginx配置 nginx -s reopen #重新启动 nginx -s quit #退出nginx
下面是我们真正使用的Linux 下 搭建Nginx,演示时我使用的WM Ware创建的虚拟机。使用putty进行远程连接。注:如果使用服务器操作的话,粘贴可就麻烦了,所以还是用远程连接吧,能直接copy paste命令
直接上链接。Nginx安装
cd /etc/yum.repos.d/
vi nginx.repo # 内容 [nginx] name=nginx repo baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/ gpgcheck=0 enabled=1 # 文档上的url是http://nginx.org/packages/mainline/OS/OSRELEASE/$basearch/ # 替换成你的 os 与 版本 # 保存退出
yum -y install nginx # 安装 systemctl enable nginx #开机自启 systemctl start nginx #启动nginx firewall-cmd --permanent --zone=public --add-port=80/tcp #永久开启80端口 firewall-cmd --reload #重新加载防火墙

nginx -t #查看配置文件路径 #结果 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
#查看上述路径的文件
user  nginx;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       /etc/nginx/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"';
    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  65;
    #gzip  on;
    include /etc/nginx/conf.d/*.conf;
}server {
    listen       80;
    server_name  localhost;
    location / {
        #root   /usr/share/nginx/html;
        root   /usr/share/nginx/html/dist/demo;
        #更改成我们上传的目录一定要写到有index.html那一级
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}nginx -s reload

前端需要调用后端的Rest API,我们需要将一部分请求配置反向代理。
server {
    listen       80;
    server_name  localhost;
    location / {
        root   /usr/share/nginx/html/dist/demo;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
        # 匹配到/proxy/这个url的时候代理到220.181.112.244 百度这个服务器
    location ^~ /proxy/ {
        proxy_set_header Host 220.181.112.244;
        proxy_set_header X-Real-IP  220.181.112.244;
        proxy_pass http://220.181.112.244/proxy/;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
    }
}注:本地项目,没有配置路由,所有会没有proxy这个东西,会报404错误,我们可以通过查看当前404是哪个服务器包的错,来判断是否发生反向代理
从图中可以看出,此时没有进行反向代理,在虚拟机的服务器上提示404

注:这里说明一点,就是即便发生了法相贷,但是network中的显示还是我的虚拟机的ip,所以不能当做是否发生反向代理的标注
nginx -s reload

一个简单的反向代理就配置好了。
注:20190220更新
1.一台虚拟机 2.虚拟机安装docker
docker pull nginx
docker run --name nginx-test-1 -d -p 8888:80 nginx /bin/bash docker run --name nginx-test-2 -d -p 8889:80 nginx /bin/bash
docker exec -it nginx-test-1 /bin/bash 内容改成 this is nginx1 docker exec -it nginx-test-2 /bin/bash 内容改成 this is nginx2
server{
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        proxy_pass http://balance;  #balance 是下面upstream后定义的名字
    }
}
# 在server{}外面增加如下代码
upstream balance { 
    server localhost:8888 weight=1; 
    server localhost:8889 weight=1; 
}curl localhost # 会发现this is nginx1 、 this is nginx2交替出现
下面关于Nginx的文章您也可能喜欢,不妨参考下:
Nginx 的详细介绍:请点这里
Nginx 的下载地址:请点这里