一文帶你走完Nginx的配置方法(Nginx+Flask)

作爲一個南方人,我每天都洗澡,一年四季,從不間斷。甚至我在北京讀書的7年,都這麼過來的(除開幾次喝醉的情況)。洗澡是一件很舒服的事情,你可以完全放鬆,聽着music,哼着小曲,多麼愜意。阿基米德也是在洗澡的時候發現了浮力,以此證明:洗澡是件好事!

今天在洗澡的時候,我突然領悟到了nginx的反向代理是幹嘛的,於是我哐哧哐哧的研究了一晚上怎麼配置nginx。以下上乾貨!

 

一、安裝(ubuntu16.04)

(1)下載

wget wget http://nginx.org/download/nginx-1.17.8.tar.gz(版本詳情請移步nginx官網

(2)安裝環境

sh:apt update

sh:apt install  libpcre3 libpcre3-dev openssl libssl-dev zlib*

(3)解壓(我的路徑/opt/nginx/)

tar -zxf nginx-1.17.8.tar.gz

(4)編譯

cd nginx-1.17.8

./configure --prefix=./main --sbin-path=/usr/local/nginx/sbin/nginx

make & make install

(5)配置文件

cd main/conf & vim my_conf.conf


二、配置nginx

 


#user  nobody;
worker_processes  2;

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

pid        logs/nginx.pid;


events {
    use epoll;
    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;
    gzip on;
    #當返回內容大於此值時纔會使用gzip進行壓縮,以K爲單位
    gzip_min_length 2k;
    #申請32 * 4K內存頁
    gzip_buffers 32 4k;
    gzip_http_version 1.0;
    #設置gzip壓縮級別,級別越底壓縮速度越快文件壓縮比越小,反之速度越慢文件壓縮比越大
    gzip_comp_level 3;
    #壓縮類型
    gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
    gzip_vary on;

    #http_proxy
    client_max_body_size 10m;
    client_body_buffer_size 128k;
    proxy_connect_timeout 60;
    proxy_send_timeout 60;
    proxy_read_timeout 60;
    proxy_buffer_size 4k;
    proxy_buffers 4 32k;
    proxy_busy_buffers_size 64k;
    proxy_temp_file_write_size 64k;
    proxy_temp_path /opt/nginx/nginx-1.17.8/main/logs 1 2;


    upstream myservers {
        #集羣共享內存的大小
        zone upstream_dynamic 64k;

        #weight:權重
        server 172.26.91.177:2331      weight=5;

        #fail_timeout:超過5s(默認10s)標記爲失聯,slow_start:30s內權重逐漸恢復至正常
        #server backend2.example.com:8080 fail_timeout=5s slow_start=30s;

        #嘗試重連次數3次(默認每次10s)
        #server 192.0.2.1                 max_fails=3;

        #自動監控backend3.example.com 域名ip的變化
        #server backend3.example.com      resolve;

        #備份服務器
        server 172.26.91.178:2332  backup;
        #server backup2.example.com:8080  backup;
        }

    upstream myservers2 {
        #集羣共享內存的大小
        zone upstream_dynamic 64k;

        #weight:權重
        server 172.26.91.177:2333      weight=5;

        #fail_timeout:超過5s(默認10s)標記爲失聯,slow_start:30s內權重逐漸恢復至正常
        #server backend2.example.com:8080 fail_timeout=5s slow_start=30s;

        #嘗試重連次數3次(默認每次10s)
        #server 192.0.2.1                 max_fails=3;

        #自動監控backend3.example.com 域名ip的變化
        #server backend3.example.com      resolve;

        #備份服務器
        server 172.26.91.178:2334  backup;
        #server backup2.example.com:8080  backup;
        }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location /web {
            proxy_pass http://myservers;
        }

        location /test {
            proxy_pass http://myservers2;
        }

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

}

以上配置,我一個一個給大家解釋,大家以後配置nginx,都可以根據以上做修改

我有兩個服務器,分別是172.26.91.177和172.26.91.178,其中每個ip開了兩個服務:

(1):http://172.26.91.177:2331/web1             和        http://172.26.91.177:2333/test1  ,分別模擬兩個類型的服務

(2):http://172.26.91.178:2332/web1             和        http://172.26.91.178:2334/test1  ,分別作爲177的備用機器

 

1、worker_processes

進程數,nginx處理任務時調用的進程數,理論上大一些肯定速度要快一些

2、error_log

日誌名字和路徑

3、pid

進程文件

4、events

(1)use epoll

使用epoll批處理方法

(2)worker_connections 1024

最大連接數

(3)multi_accept on

允許一個進程同時接受多個網絡連接

5、http

(1)include     mime.types

MIME.type解釋

(2)default_type   application/octet-stream

普通文件流

(3)sendfile   on

開啓高效文件傳輸模式

(4)keepalive_timeout 60

請求超時時間

(5)壓縮

gzip on;
#當返回內容大於此值時纔會使用gzip進行壓縮,以K爲單位
gzip_min_length 2k;
#申請32 * 4K內存頁
gzip_buffers 32 4k;
#向上兼容
gzip_http_version 1.0;
#設置gzip壓縮級別,級別越底壓縮速度越快文件壓縮比越小,反之速度越慢文件壓縮比越大
gzip_comp_level 3;
#壓縮類型
gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
#開啓根據請求頭判斷是否壓縮
gzip_vary on;

(6)代理(看字識義。。。)

client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 60;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_temp_path /opt/nginx/nginx-1.17.8/main/logs 1 2;

(7)負載均衡核心配置1(服務器配置)

upstream myservers {
        #集羣共享內存的大小
        zone upstream_dynamic 64k;

        #weight:權重
        server 172.26.91.177:2331      weight=5;

        #fail_timeout:超過5s(默認10s)標記爲失聯,slow_start:30s內權重逐漸恢復至正常
        #server backend2.example.com:8080 fail_timeout=5s slow_start=30s;

        #嘗試重連次數3次(默認每次10s)
        #server 192.0.2.1                 max_fails=3;

        #自動監控backend3.example.com 域名ip的變化
        #server backend3.example.com      resolve;

        #備份服務器
        server 172.26.91.178:2332  backup;
        #server backup2.example.com:8080  backup;
        }

    upstream myservers2 {
        #集羣共享內存的大小
        zone upstream_dynamic 64k;

        #weight:權重
        server 172.26.91.177:2333      weight=5;

        #fail_timeout:超過5s(默認10s)標記爲失聯,slow_start:30s內權重逐漸恢復至正常
        #server backend2.example.com:8080 fail_timeout=5s slow_start=30s;

        #嘗試重連次數3次(默認每次10s)
        #server 192.0.2.1                 max_fails=3;

        #自動監控backend3.example.com 域名ip的變化
        #server backend3.example.com      resolve;

        #備份服務器
        server 172.26.91.178:2334  backup;
        #server backup2.example.com:8080  backup;
        }

1)upstream聲明進行配置,myservers自定義別名,代表一個配置(服務)的名稱

2)zone upstream_dynamic 64k

共享內存大小

3)其餘見註釋備註

(8)負載均衡核心配置2(代理規則)

server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location /web {
            proxy_pass http://myservers;
        }

        location /test {
            proxy_pass http://myservers2;
        }

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

上面兩個分塊的配置,我是要達到如下要求:

主服務器(177),有兩個服務,請求的url分別爲:

http://172.26.91.177:2331/web1                    http://172.26.91.177:2333/test1

如果訪問web1子路徑,則nginx轉發到2331端口,如果訪問test1子路徑,則轉發到2333端口

另外,如果以上服務宕了,

http://172.26.91.178:2332/web1                    http://172.26.91.178:2334/test1  

分別作爲177的備用機器

這樣,就把備份和負載均衡全部設置在了一起,配合容器編排,能毫無壓力的搭建高併發的web服務!

這樣的nginx模板能做到:

服務轉發到相同機器的相同端口的不同子域名

服務轉發到相同機器的不同端口的不同子域名

服務轉發到相同機器的不同端口的相同子域名

服務轉發到不同機器的相同端口的不同子域名

服務轉發到不同機器的不同端口的不同子域名

服務轉發到不同機器的不同端口的相同子域名

服務轉發到不同機器的相同端口的相同子域名

你就說厲不厲害吧!!!

 

三、nginx常用命令

(1)啓動(指定配置文件,推薦)

nginx -c xxxxx/my.conf

(2)重載(一般用於修改文件後)

nginx -s reload

(3)關閉

nginx -s quit

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