Ubuntu安裝Nginx 配置反向代理、負載均衡、PHP、Go語言代理

安裝

sudo apt-get install nginxphp

看一下安裝的結果:css

image.png

默認安裝位置:html

/usr/sbin/nginx:主程序
/etc/nginx:存放配置文件
/usr/share/nginx:存放靜態文件
/var/log/nginx:存放日誌
複製代碼

指令node

啓動nginx
中止nginx -s stop
重載nginx -s reload
複製代碼

配置文件mysql

vi /etc/nginx/nginx.conf
vi /etc/nginx/sites-enabled/default 
複製代碼

主配置文件

worker_processes auto;     #CPU核心數
worker_rlimit_nofile 65535; # ulimit -n

events {
 multi_accept on;
 use epoll;
 worker_connections 1024;
 }

http {
 sendfile on; #默認就是on的
 aio on; 
 directio 4m;
 tcp_nopush on;
 tcp_nodelay on;
 }

複製代碼

image.png

server

server {
        listen 80; # 監聽端口
        server_name  www.xxxxx.com; #監聽域名/主機名/IP

        location / {
                root  html;   #/usr/share/nginx/html;
				index index.html index.htm;
        }
        
        location ~/myweb { #區分大小寫,即訪問Myweb是不行的
        root html/localtion;  #定義myweb所在的路徑,即在瀏覽器訪問myweb的時候,實際是訪問的html/localtion/myweb目錄裏面的web內容
        index   index.html; #默認首頁文件類型
    	}
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
         }
}
複製代碼

虛擬主機(多IP、多port、多host域名) server_name localhost www.a.com; #多個域名用空格間隔便可 server_name 192.168.0.2; #IP是本機的網卡IP地址android

多域名:nginx

server {
        listen 80;
        server_name www.abc.com;
        location / {
                proxy_pass http://192.168.1.10;
        }
     }
 
    server {
        listen 80;
        server_name image.test.com;
        location / {
                alias /var/www/image;
        }
     }
 
    server {
        listen 80;
        server_name video.abc.com;
        location / {
                proxy_pass http://192.168.1.10:8081/video;
        }
     }
複製代碼

在上述配置中,將三個域名分發給了不一樣的模塊處理:web

location

/ 通用匹配  至關於default
=  #用於標準uri前,須要請求字串與uri徹底匹配,若是匹配成功就中止向下匹配並當即處理請求。
~  #區分大小寫
~*  #不區分大寫
!~  #區分大小寫不匹配
!~* #不區分大小寫不匹配 
^  #匹配以什麼開頭
$  #匹配以什麼結尾
\  #轉義字符。能夠轉. * ?等
*  #表明任意長度的任意字符

-f和!-f #用來判斷是否存在文件
-d和!-d #用來判斷是否存在目錄
-e和!-e #用來判斷是否存在文件或目錄
-x和!-x #用來判斷文件是否可執行
複製代碼

列出目錄sql

location /images {
root /var/www/nginx-default/images;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}
複製代碼

靜態資源直接處理shell

location ~ \.(css|js|fonts|png|svg|html|txt)$ {
        #access_log on;
        expires 1d; #過時時間
 
        root /newhome/go/gowork/goonlinemusic/static;
        try_files $uri $uri/;
 }
複製代碼

不一樣瀏覽器

upstream android {
        server 172.16.1.7:9090;
}

upstream iphone {
        server 172.16.1.7:9091;
}

upstream pc {
        server 172.16.1.7:9092;
}

server {
        listen 80;
        server_name sj.drz.com;
        charset 'utf-8';

        location / {

                #若是客戶端來源是Android則跳轉到Android的資源;
                if ($http_user_agent ~* "Android") {
                        proxy_pass http://android;
                }

                #若是客戶端來源是Iphone則跳轉到Iphone的資源;
                if ($http_user_agent ~* "Iphone") {
                        proxy_pass http://iphone;
                }

                #若是客戶端是IE瀏覽器則返回403錯誤;
                if ($http_user_agent ~* "MSIE") {
                        return 403;
                }

                #默認跳轉pc資源;
                proxy_pass http://pc;
        }
}
複製代碼

setheader

server {
    listen 80;
    server_name www.xxx.com;

    location /xxx/
    {
    proxy_pass http://192.168.1.10:8080/xxx/;
    proxy_set_header host $host;
    proxy_set_header X-Real-IP      $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}
複製代碼

go server

## 設置反向代理,指向go服務器 ##
server {
    listen 8081;
     
    location / {
        proxy_pass  http://localhost:9090;
        #go 服務器能夠指定到其餘的機器上,這裏設定本機服務器
     
        #Proxy Settings
        proxy_redirect     off;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

    }
}
## End 設置反向代理,指向go服務器 ##
複製代碼

負載均衡

##
# Upstream Settings
##
upstream test { 
	server 127.0.0.1:777 weight=5; 
	server 127.0.0.1:5000 weight=5;  
	server 192.168.12.100:5000 fail_timeout=2s max_fails=1;
}

server {
    listen 8081;
    
    location / {
        proxy_pass  http://test;
     
        #Proxy Settings
        proxy_redirect     off;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

    }
}
複製代碼

fail_timeout 設置的時間內服務器沒有響應則認爲服務器失效,默認10s max_fails 容許鏈接失敗的次數,默認1 fail_time 服務器被移除輪詢隊列的時間,默認10s(在移除的時間內再也不被輪詢請求) backup 標記該服務器爲備用服務器,當主服務器宕機時,它將會代替主服務器接收請求 down 標記此服務器永久停機

HTTPS

server {
        listen       443 ssl;
        server_name  ssl.xxxxx.com; #綁定證書的域名

        ssl_certificate      1_ssl.xxxxx.com_bundle.crt;#絕對路徑
        ssl_certificate_key  2_ssl.xxxxx.com.key; #改成絕對路徑

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
    	ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照這個協議配置

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location / {
            proxy_pass http://127.0.0.1:8886;
        }
    }
複製代碼

image.png

支持PHP

安裝

sudo apt-get install php7.2 php7.2-fpm php-json php-curl php7.2-mysql php7.2-cgi
複製代碼

vi /etc/nginx/sites-enabled/default

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /root/www/default;                                                                            

    index index.php index.html;

    server_name _;

    location / {
        try_files $uri $uri/ =404;
    }

    # pass PHP scripts to FastCGI server
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;

        # With php-fpm (or other unix sockets):
        #fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        # With php-cgi (or other tcp sockets):
        fastcgi_pass 127.0.0.1:9000;
    }

}
複製代碼

vi /etc/php/7.2/fpm/pool.d/www.conf

36 listen = 127.0.0.1:9000
複製代碼

service nginx restart

service php7.2-fpm restart

相關文章
相關標籤/搜索