Nginx安裝及配置反向代理


本片博客記錄在ubuntu16下安裝nginx,以及如何實現負載均衡

安裝nginx

  • 如果是新機器,安裝相關依賴環境
sudo apt install build-essential
sudo apt install libtool
sudo apt install libpcre3 libpcre3-dev
sudo apt install zlib1g-dev
sudo apt-get install openssl libssl-dev

如果沒有依賴環境,編譯的時候會報下面的錯

make: *** No rule to make target 'build', needed by 'default'.  Stop.

2 下載nginx 到虛擬機

當然我是直接通過filezilla扔給虛擬機的

3 解壓,順道刪除壓縮包

tar xvf XXX

rm -rf XXX

4 進入nginx目錄里面,指定安裝的目錄

指定安裝在 /opt/nginx目錄下(一般都把自己的軟件安裝在opt下)

./configure --prefix=/opt/nginx --sbin-path=/usr/bin/nginx

5 編譯安裝

make && make install

6 啟動

 nginx

ok ,現在可以去訪問虛擬機的80端口,成功看到nginx的 歡迎頁

7 停止

nginx -s stop

nginx號稱7x24小時不停止,所以當我們修改配置文件之后,重新加載nginx,執行下面的腳本就好了

nginx -s reload

反向代理

1. 什么是正向代理?:

先說一下啥是正向代理,其實我們用的vpn本質上就是正向代理,因為一堵牆圍着我們國家,所以我們通過瀏覽器訪問國外的谷歌時請求被強了,根本出不去,那么我們怎么請求呢?vpn出現了,他就是一種正向代理,我們把我們的請求發送給vpn所在的服務器,比如它在香港,讓他幫我們轉發給谷歌,谷歌接收到請求后把數據回顯給vpn,vpn發送給我們,這其實就是一個正向代理的過程

  • 正向代理特點: 代理服務器起到一個轉發請求的作用,但是從一開始它就很明確的知道自己要去訪問哪台服務器

2. 什么是反向代理?

  • 同樣它的工作也是負責轉發;來自客戶端的請求,但是當客戶端的請求發過來之后,一開始它是不清楚往哪里轉發的,他需要根據配置去解析,然后再定位,轉發

3. 為什么要使用反向代理?

現在流行的微服務架構,擁有相同功能的服務,通常需要做成集群,那么問題來了,我們的瀏覽器默認是80端口,多個功能相同的應用分別占用多個不同的端口,那誰來占用80呢?nginx通過反向代理優秀的解決了這個問題
{% asset_img 1.jpg my first image %}

4. nginx如何實現反向代理

通過配置實現

nginx的主配置文件名叫

nginx.config
它在nginx的安裝目錄下的config目錄下面


#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塊主要有三個作用域, http  server  location
http {	#可以嵌套多個server ,配置代理,緩存,日志,等絕大多數功能,和第三方模塊配置
    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;

# 開啟高效文件傳輸模式,這個指令,指定了nginx是否調用sendfile函數傳輸文件,對於普通的應用設為on ,如果用來進行下載等IO重負載應用可設為off
    sendfile        on;
    #tcp_nopush     on;  #防止網絡阻塞

    #keepalive_timeout  0;
    keepalive_timeout  65;  # 長連接超時時常,單位秒

    #gzip  on;

# 負載均衡
#    upstream XXX.com{
#   upstream負載均衡塊,  weight表示權重, 值越大,被分配到的幾率就越大
#    server 192.168.80.121:80 weight=3;
#    server 192.168.80.121:80 weight=3;	
#    server 192.168.80.121:80 weight=3;
#}


#server配置虛擬主機的相關參數
    server {  
        listen       80; # 監聽端口

        server_name  localhost; # 監聽地址,可以采用域名加多個空格隔開,如果匹配不到我們在瀏覽器輸入的域名.默認走 localhost, 然后location到nginx的歡迎頁
	
        #charset koi8-r;

        # 本虛擬機的訪問日志	
        #access_log  logs/host.access.log  main;

# location 配置請求的路由,和各種頁面的處理情況

        location / {
            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;
        #}
    }


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

}

我們要實現反向代理就是要

  • 新增我們自己的server
	server {
        listen       80;
        server_name  瀏覽器可能訪問的域名1;

        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        location / {
			proxy_pass http://127.0.0.1:9001;   # 轉發路徑
			proxy_connect_timeout 600;
			proxy_read_timeout 600;
        }
    }
	server {
        listen       80;
        server_name  瀏覽器可能訪問的域名2;

        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        location / {
			proxy_pass http://127.0.0.1:10011;  # 轉發路徑
			proxy_connect_timeout 600;
			proxy_read_timeout 600;
        }
    }

nginx 通過監聽80端口 進而監聽我們修改后的域名,成功匹配后呢,通過location轉發到我們自定義的路徑


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM