2020-01-16 Nginx配置设置

一、nginx 配置 访问静态资源

目标:想要尝试用IP的方式来访问  D:\test\imgs  下的资源

  

 

 

 

 

 

 

 如上图配置,配置好nginx.conf文件。

在浏览器上输入:http://localhost:10080/imgs/

得到结果:

 

 

 注意:一定要在最后加上"/"   http://localhost:10080/imgs/

要不然会去访问这个路径 D:\JavaTools\nginx-1.16.1/html/imgs

出现访问不到资源,可以在这里找到错误信息

 

 

二、Nginx配置负载均衡

1、先开两个项目

打包springboot项目成jar包,扔到这里。命令窗口执行:java -jar sb-flexible-a-1.0.jar 

 

 

 

 

 

 另外一个在IDEA运行,改端口为8082

 

 

 

启动完成之后,两个项目都访问这个方法。

 

 

 效果:

 

 

 

 

 

 

 

 

 

 

 两个项目都正常运行。

2、开始配置Nginx的nginx.conf文件。

加入 

upstream pancm{
   server 127.0.0.1:8081;
   server 127.0.0.1:8082;
}
pancm 这个起名随意,也可以为"name.pancm".

location部分的配置
proxy_pass:代理路径,一般配置upstream后面的名称用于实现负载均衡,可以直接配置ip进行跳转;
location / {
            root   html;
        proxy_pass http://pancm;
            index  index.html index.htm;
        }

nginx.conf文件的所有内容

#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;

    upstream pancm{
       server 127.0.0.1:8081;
       server 127.0.0.1:8082;
    }

    server {
        listen       10080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
        proxy_pass http://pancm;
            index  index.html index.htm;
        }
    
    #访问路径拼接 upload 访问本地绝对路径下的某图片 
    #这里的alias,可以理解为:/imgs/ =alias配置的路径
    location /imgs/ {
            alias   D:/test/imgs/;
        autoindex on;
        }

        #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,输入这个:
http://127.0.0.1:10080/flexible/test/fun1
可以看到以下效果,轮流去调用不同项目的接口。

 

 相当于

http://127.0.0.1:10080/flexible/test/fun1  会去随机调用

http://127.0.0.1:8081/flexible/test/fun1http://127.0.0.1:8082/flexible/test/fun1 这两个。

如果其中一个项目挂掉了,就会自动一直去调用另一个,并不会访问不到资源。两个都挂掉了,就会访问不到资源。

 

这里只是随便用下负载均衡。

如果还要详细一点配置,可以参考官方的。

posted @ 2020-01-16 10:23  math_lin  阅读(517)  评论(0)    收藏  举报