nginx的总结篇以及一台nginx代理多个资源服务

Posted 健康平安的活着

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx的总结篇以及一台nginx代理多个资源服务相关的知识,希望对你有一定的参考价值。

nginx的概念和作用

1.1 概念

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

1.2 作用

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

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

3.解决跨域

4.配置虚拟机。

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

1.3 启动关闭

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

2.查看nginx的进程

 3.关闭

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

./nginx -s stop

退出命令:

./nginx -s quit

等程序执行完毕后关闭,建议使用此命令。

4.重新加载

./nginx -s reload

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

二 静态资源代理

2.1 nginx的配置

1.config的配置

 2.存放的实际资源

3.访问页面

三 虚拟主机

四  同一台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;
    #    }
    #}

}

以上是关于nginx的总结篇以及一台nginx代理多个资源服务的主要内容,如果未能解决你的问题,请参考以下文章

详解Nginx + Tomcat 反向代理 如何在高效的在一台服务器部署多个站点

详解Nginx + Tomcat 反向代理 如何在高效的在一台服务器部署多个站点

详解Nginx + Tomcat 反向代理 如何在高效的在一台服务器部署多个站点

nginx如何实现负载均衡以及实现方式

nginx 反向代理说明

nginx的反向代理配置