分佈式架構之Nginx 使用

一、入門

1、簡介:它是高性能的web和反向代理服務器。

二、安裝

1、普通版本安裝

  • 先安裝輔助工具
#安裝 make
yum -y install gcc automake autoconf libtool make
#安裝 g++
yum install gcc gcc-c++
#安裝 pcre,爲了重寫rewrite
yum install -y pcre pcre-devel
#安裝 zlib,爲了gzip壓縮
yum install -y zlib zlib-devel
#安裝 openssl,安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及SSL協議
yum install -y openssl openssl-devel
#解壓
tar -zxvf nginx-1.10.3.tar.gz
cd nginx-1.10.3
# 然後增加配置
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi
#安裝,還是到 nginx-1.10.3 目錄下
make
make install
  • 成功後看一下 /usr/local/nginx 目錄
  • 啓動
#啓動前先創建 /var/temp/nginx 目錄,因爲剛纔安裝時配置是用到這個目錄,接着再啓動
cd /usr/local/nginx/sbin
./nginx

#如果是關閉,則是
./nginx stop
#如果是重啓,則是
./nginx -s reload
  • 查看進程是否正常啓動,然後訪問瀏覽器,默認是80端口

   

   

2、docker版本安裝

  • docker search nginx ,看看版本
  • 拉取鏡像 docker pull docker.io/nginx
  • 完成後看一下本地鏡像 docker images nginx
  • 運行鏡像 docker run --name nginx -p 18080:80 -d docker.io/nginx ,再到瀏覽器訪問 ip/port ,看到以下界面即成功

   

  • 接着先在主機 /etc/nginx 下創建 www、logs、conf 三個文件夾如下。複製docker內的nginx配置到主機,我的容器id是55de6205da3a,複製後啓動另一個容器

  

#複製配置文件
docker cp 55de6205da3a:/etc/nginx/nginx.conf /etc/nginx/conf/
#啓動另一個容器,-v 引號前面是主機地址,後面是容器地址,表示指定的主機地址掛載到容器的地址,也就是容器訪問地址映射到主機
docker run -d -p 18082:80 --name nginx-test -v /etc/nginx/www:/usr/share/nginx/html -v /etc/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /etc/nginx/logs:/var/log/nginx docker.io/nginx
  • 到主機 /etc/nginx/www 創建一個簡單測試網頁 index.html ,訪問瀏覽器查看結果
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
    <h1>hello world</h1>
</body>
</html>

   

  • 如果是重啓可以用下面的指令
docker exec -it id nginx -s reload

三、配置(這裏演示對普通安裝的那種)

詳細請看手冊:http://www.nginx.cn/nginx-how-to

修改原有的默認配置文件 /usr/local/nginx/conf下的 nginx.conf ,屏蔽原來的 server節點,增加 include /usr/local/nginx/conf/conf.d/*.conf;

#user  nobody;
worker_processes  1;

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

pid        /usr/local/nginx/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;
    include /usr/local/nginx/conf/conf.d/*.conf;

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

}

1、配置訪問自己的主頁

  • 在 /usr/local/nginx/conf下新建 conf.d 目錄,用於統一放服務配置文件,裏面新建一個my.conf,裏面內容如下
#如果多個轉發,增加一個server節點即可
server {
    listen       80;
#如果有多個,server_name 後面的值用空格隔開
    server_name  localhost;
    location / {
        root   /usr/local/nginx/html;
        index  my.html my.htm;
    }
}
  • 到 /usr/local/nginx/html 下新建 my.html 文件
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>success</h1>
</body>
</html>
  • 到 /usr/local/nginx/sbin,./nginx -s reload ,訪問瀏覽器查看結果

 

2、正向代理

  • 修改一下我們的 my.conf 文件,同樣需要到 /usr/local/nginx/sbin,./nginx -s reload
#代理轉發到的ip
resolver 8.8.8.8;
server {
	listen 80;
	#這裏不需要 server_name節點
	location / {
		proxy_pass http://$http_host$request_uri;
	}
}

3、反向代理

  • 修改一下我們的 my.conf 文件,同樣需要到 /usr/local/nginx/sbin,./nginx -s reload
server {
    listen       80;
    server_name  localhost;
    location / {
        proxy_pass http://192.168.1.60:81;
    }
}

   

3、負載均衡

  • 同樣修改 my.conf 文件,同樣需要到 /usr/local/nginx/sbin,./nginx -s reload
#weight是權重,默認是1,越高請求數越多
upstream mytest {
   server 192.168.1.60:81 weight=2;
   server 192.168.1.60:8080;
}
#請求到端口80的將其傳遞給上游。
#上游名稱也就是上面upstream後面的名字和proxy_pass後面的http://後面的名字需要匹配。
server {
    listen       80;
    server_name  localhost;
    location / {
        proxy_pass http://mytest; 
    }
}

四、匹配原理

訪問代理機的請求(也就是瀏覽器輸入的地址):http://192.168.1.60/index
真正接口地址:http://192.168.1.6:8080/index
listen       80;
server_name  localhost;

#可以訪問
location / {
	proxy_pass http://192.168.1.6:8080;
}

#可以訪問
location /index {
	proxy_pass http://192.168.1.6:8080;
}

#不可以訪問,最後的/ 會將匹配的值也替換掉
location /index {
	proxy_pass http://192.168.1.6:8080/;
}

#不可以訪問
location / {
	proxy_pass http://192.168.1.6:8080/index;
}

#可以訪問
location /index {
	proxy_pass http://192.168.1.6:8080/index;
}

//另外的情況
訪問代理機的請求(也就是瀏覽器輸入的地址):http://192.168.1.60/api/index
真正接口地址:http://192.168.1.6:8080/api/index
listen       80;
server_name  localhost;

#可以訪問
location / {
	proxy_pass http://192.168.1.6:8080;
}

#不可以訪問
location /index {
	proxy_pass http://192.168.1.6:8080;
}

#不可以訪問
location / {
	proxy_pass http://192.168.1.6:8080/api;
}

#不可以訪問
location /index {
	proxy_pass http://192.168.1.6:8080/api;
}

#不可以訪問
location / {
	proxy_pass http://192.168.1.6:8080/api/index;
}

#不可以訪問
location /index {
	proxy_pass http://192.168.1.6:8080/api/index;
}

#可以訪問
location / {
	proxy_pass http://192.168.1.6:8080;
}

#可以訪問
location /api {
	proxy_pass http://192.168.1.6:8080;
}

#不可以訪問
location / {
	proxy_pass http://192.168.1.6:8080/api;
}

#可以訪問
location /api {
	proxy_pass http://192.168.1.6:8080/api;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章