一 nginx的概念和作用

1.1 概念

1.nginx是一个代理静态资源的高性能http服务器和反向代理的服务器。

1.2 作用

1.代理静态资源:可以做静态网页的http服务器。

2.反向代理、负载均衡。:把请求转发给不同的服务器

3.解决跨域

4.配置虚拟机。

一个域名可以被多个ip绑定。可以根据域名的不同吧请求转发给运行在不同端口的服务器

1.3 启动关闭

1.启动: 在nginx目录下有一个sbin目录,sbin目录下有一个nginx可执行程序: ./nginx

2.查看nginx的进程

NGINX 多个流量转发 nginx转发给多台服务器_NGINX 多个流量转发

 3.关闭

关闭命令:相当于找到nginx进程kill。

./nginx -s stop
退出命令:
./nginx -s quit
等程序执行完毕后关闭,建议使用此命令。
4.重新加载
./nginx -s reload

可以不关闭nginx的情况下更新配置文件。

二 静态资源代理

2.1 nginx的配置

1.config的配置

NGINX 多个流量转发 nginx转发给多台服务器_NGINX 多个流量转发_02

 2.存放的实际资源

NGINX 多个流量转发 nginx转发给多台服务器_运维_03

3.访问页面

NGINX 多个流量转发 nginx转发给多台服务器_html_04

三 虚拟主机

3.1 同一台nginx,不同端口号虚拟两台服务器(ip)

nginx的配置文件:

1.第一个server块

NGINX 多个流量转发 nginx转发给多台服务器_NGINX 多个流量转发_05

2.第二个server块: 

NGINX 多个流量转发 nginx转发给多台服务器_nginx_06

 3.资源位置:

NGINX 多个流量转发 nginx转发给多台服务器_NGINX 多个流量转发_07

访问80:

NGINX 多个流量转发 nginx转发给多台服务器_html_08

 访问8081:

NGINX 多个流量转发 nginx转发给多台服务器_NGINX 多个流量转发_09

3.2  使用域名存储静态资源

1.配置nginx的配置文件

NGINX 多个流量转发 nginx转发给多台服务器_运维_10

2.修改host文件

NGINX 多个流量转发 nginx转发给多台服务器_html_11

3.启动服务,进行访问 

NGINX 多个流量转发 nginx转发给多台服务器_html_12

3.3 同一台nginx,相同端口,虚拟两台不同域名的服务器(域名)

1.修改nginx配置文件

NGINX 多个流量转发 nginx转发给多台服务器_NGINX 多个流量转发_13

NGINX 多个流量转发 nginx转发给多台服务器_html_14

2.host文件配置

NGINX 多个流量转发 nginx转发给多台服务器_运维_15

 3.页面访问,相同端口,不同域名,进行访问

NGINX 多个流量转发 nginx转发给多台服务器_nginx_16

 

NGINX 多个流量转发 nginx转发给多台服务器_html_17

四 反向代理:8080端口代理成80端口

tomcat的服务访问地址: http://localhost:8080/nginx-demo/index.html

被代理后的地址: http://localhost/nginx-demo/index.html

4.1 nginx配置

NGINX 多个流量转发 nginx转发给多台服务器_服务器_18

 4.2 tomcat的启动

NGINX 多个流量转发 nginx转发给多台服务器_html_19

 4.3  访问

NGINX 多个流量转发 nginx转发给多台服务器_服务器_20

五 负载均衡

六   同一台nginx代理多个不同的静态资源

80端口 下 存储 vue的静态页面:   html/dky-guolu-vue

新建一个server块,端口为8099,创建一个存储react静态页面:   html/dky-guolu-react/;

#前端react页面
         server {
         listen       8099;
         server_name  localhost;        location / {
             root   html/dky-guolu-react/;
             index  index.html index.htm;
         }
         error_page   500 502 503 504  /50x.html;
         location = /50x.html {
             root   html;
         }
     }并且将react代理到80端口下,配置如下配置 :
 #前端react的页面
         location /react {
             proxy_pass http://32.32.255.239:8099/;
             #root   html;
             #index  index.html index.htm;
         }

访问地址:http://32.32.255.239:80/react/index.html

原先地址:http://32.32.255.239:8099/index.html

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
       
      #悬挂vue菜单页面
        location / {
            #proxy_pass http://172.16.71.134:8080;
            root   html/dky-guolu-vue;
            index  index.html index.htm;
        }
        #门户前端
       location /frame/ {
	        proxy_pass   http://32.32.255.239:8989/frame/;
	         #root   html;
                 #index  index.html index.htm;
        }
        #调取cloudiip 
        location /cloudiip-cas {
     	         proxy_pass   http://32.32.255.241:80/;
         	 #root   html;
            	 #index  index.html index.htm;
        }
        #代理综合爆炸图-一次风机
         location /zhbz-3ycfj  {
                 proxy_pass   http://32.32.255.239:8098/;
                 #root   html;
                 #index  index.html index.htm;
        }
        #代理综合爆炸图-送风机
         location /zhbz-3sfj  {
                 proxy_pass   http://32.32.255.239:8096/;
                 #root   html;
                 #index  index.html index.htm;
        }
        #代理综合爆炸图-一次风机
         location /zhbz-3yfj  {
                 proxy_pass   http://32.32.255.239:8095/;
                 #root   html;
                 #index  index.html index.htm;
        }

        #前端react的页面
        location /react {
            proxy_pass http://32.32.255.239:8099/;
            #root   html;
            #index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
#综合-爆炸图-一次风机
	server {
        listen       8098;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html/dky-guolu-vue/webgl/3ycfj;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

#综合-爆炸图-送风机
        server {
        listen       8096;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html/dky-guolu-vue/webgl/3sfj;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
#综合-爆炸图-引风机
        server {
        listen       8095;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html/dky-guolu-vue/webgl/3yfj;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }






	#综合脱硫图
	server {
        listen       8097;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html/dky-guolu-vue/webgl/webGL2;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
#前端react页面
        server {
        listen       8099;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html/dky-guolu-react/;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}