Nginx

一,經常使用指令

在sbin目錄下html

查看版本號:./nginx -vjava

啓動nginx:./nginxlinux

關閉nginx:./nginx -s stopnginx

重啓nginx:./nginx -s reloadweb

二,反向代理

1,訪問www.xiaoming.com(nginx服務器),鏈接到http://127.0.0.1:8080(tomcat服務器)後端

在linux的hosts文件中加入:tomcat

192.168.0.101 www.xiaoming.com

打開nginx目錄下conf/nginx.conf,修改配置:服務器

server {
        listen       80;
        #server_name  localhost;
        server_name 192.168.0.101;


        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://127.0.0.1:8080;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

2,訪問不一樣的資源跳轉到不一樣讀服務器app

訪問www.xiaoming.com/vod/a.html跳轉到服務器1,訪問www.xiaoming.com/edu/a.html跳轉帶服務器2。負載均衡

在兩臺tomcat服務器的webapp目錄下分別新建vod目錄和edu目錄,而且在目錄下新建a.html

更改nginx.conf

server {
        listen       80;
        server_name 192.168.0.101;
        location ~ /vod/ {
            proxy_pass http://192.168.0.101:8080;
        }
        location ~ /edu/ {
            proxy_pass http://59.67.77.90:8080;
        }
    }

三,負載均衡

屢次訪問同一個地址,分別跳轉到不一樣的服務器上

在兩個tomcat目錄的webapp下分別新建vod目錄,下面新建a.html

配置nginx.conf:

upstream myserver {
        server 192.168.0.101:8080;
        server 59.67.77.90:8080;

    }
    server {
        listen       80;
        server_name 192.168.0.101;
        location / {
            proxy_pass http://myserver;
        }
    }

ngnix的負載均衡策略:

輪尋(默認)

按權重輪尋:

upstream myserver {
        server 192.168.0.101:8080 weight=1;
        server 59.67.77.90:8080 weight=2;
    }

ip_hash(根據ip地址hash,固定每個ip訪問同一個服務器):

upstream myserver {
        ip_hash;
        server 192.168.0.101:8080;
        server 59.67.77.90:8080;
    }

fair(按照後端服務器的響應時間分配,時間越短權重越高):

upstream myserver {
        fair;
        server 192.168.0.101:8080;
        server 59.67.77.90:8080;
    }

四,動靜分離

location匹配規則:https://blog.csdn.net/jy02149522/article/details/79066574

正則:https://www.runoob.com/regexp/regexp-syntax.html

在主機1(nginx所在的主機)的/usr/local/static/pic下放置圖片fuzai.png

在主機2的tomcat目錄/webapp/vod下放置test.jsp,內容以下:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<HTML>
    <HEAD>
        <TITLE>JSP Test Page</TITLE>
    </HEAD>
    <BODY>
        <h1>test</h1>
            <%
            Random rand = new Random();
            out.println("<h1>Random number:</h1>");
            out.println(rand.nextInt(99)+100);
            %>
        <img src="http://www.xiaoming.com/pic/fuzai.png">
    </BODY>

</HTML>

配置nginx.conf:

server {
        listen       80;
        server_name 192.168.0.101;
        location ~ \.(png|jpg|gif)$ {
            root /usr/local/static;
        }
        location / {
            proxy_pass http://59.67.77.90:8080;
        }
    }

訪問www.xiaoming.com/vod/test.jsp:

相關文章
相關標籤/搜索