1. 程式人生 > >nginx反向代理服務器以及負載均衡,從安裝到配置

nginx反向代理服務器以及負載均衡,從安裝到配置

配置文件 scripts 退出vim 錯誤碼 重啟 cal logs 點擊 listening

nginx的具體作用不用細說,很強大,做負載均衡、反向代理服務器解決前端跨域問題等等。下面是nginx的安裝過程

首先nginx主要的依賴:

1 pcre、 pcre-devel 
2 zlib zlib-devel 
3 openssl openssl-devel 

因此安裝nginx需要安裝以下依賴(本教程只介紹centos在線安裝,離線安裝見另一篇文章)

1.安裝依賴

1 yum install gcc
2 yum install pcre-devel
3 yum install zlib zlib-devel
4 yum install openssl openssl-devel

2.下載解壓nginx

2 cd /usr/local
3 mkdir nginx
4 cd nginx
5 //下載tar包
6 wget http://nginx.org/download/nginx-1.13.7.tar.gz
7 tar -xvf nginx-1.13.7.tar.gz             (解壓文件)

3.安裝nginx

1 //進入nginx目錄
2 cd /usr/local/nginx-1.13.7
3 //執行命令
4 ./configure
5 //執行make命令
6 make
7 //執行make install命令
8 make install

4.配置nginx

cd到nginx配置文件,配置相應的信息數據,主要是配置好upstream 以及server即可,現在貼出我本機的配置

cd /usr/local/nginx/conf
vim nginx.conf

技術分享圖片

5.測試nginx是否配置正確

/usr/local/nginx/sbin/nginx  -t

如果出現以下所示,則配置沒有問題

技術分享圖片

6.nginx的啟停

啟動
[root@localhost ~]# /usr/local/nginx/sbin/nginx
停止/重啟
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s stop(quit、reload)
命令幫助
[root@localhost 
~]# /usr/local/nginx/sbin/nginx -h 驗證配置文件 [root@localhost ~]# /usr/local/nginx/sbin/nginx -t 配置文件 [root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

其他介紹

1.VIM語法

默認vim打開後是不能錄入的,需要按鍵才能操作,具體如下:
開啟編輯:按“i”或者“Insert”鍵
退出編輯:“Esc”鍵
退出vim:“:q”
保存vim:“:w”
保存退出vim:“:wq”
不保存退出vim:“:q!”

2.nginx配置語法介紹

  1 #user  nobody;  
  2 
  3 #開啟進程數 <=CPU數   
  4 worker_processes  1;  
  5 
  6 #錯誤日誌保存位置  
  7 #error_log  logs/error.log;  
  8 #error_log  logs/error.log  notice;  
  9 #error_log  logs/error.log  info;  
 10 
 11 #進程號保存文件  
 12 #pid        logs/nginx.pid;  
 13 
 14 #每個進程最大連接數(最大連接=連接數x進程數)每個worker允許同時產生多少個鏈接,默認1024  
 15 events {  
 16     worker_connections  1024;  
 17 }  
 18 
 19 
 20 http {  
 21     #文件擴展名與文件類型映射表  
 22     include       mime.types;  
 23     #默認文件類型  
 24     default_type  application/octet-stream;  
 25 
 26     #日誌文件輸出格式 這個位置相於全局設置  
 27     #log_format  main  ‘$remote_addr - $remote_user [$time_local] "$request" ‘  
 28     #                  ‘$status $body_bytes_sent "$http_referer" ‘  
 29     #                  ‘"$http_user_agent" "$http_x_forwarded_for"‘;  
 30 
 31     #請求日誌保存位置  
 32     #access_log  logs/access.log  main;  
 33 
 34     #打開發送文件  
 35     sendfile        on;  
 36     #tcp_nopush     on;  
 37 
 38     #keepalive_timeout  0;  
 39     #連接超時時間  
 40     keepalive_timeout  65;  
 41 
 42     #打開gzip壓縮  
 43     #gzip  on;  
 44 
 45     server {  
 46         #監聽端口,默認是80端口  
 47         listen       80;  
 48         #監聽域名  
 49         server_name  localhost;  
 50 
 51         #charset koi8-r;  
 52 
 53         #nginx訪問日誌放在logs/host.access.log下,並且使用main格式(還可以自定義格式)  
 54         #access_log  logs/host.access.log  main;  
 55 
 56         #如果沒有location更明確的匹配訪問路徑的話,訪問請求都會被該location處理。  
 57         location / {  
 58             #root指定nginx的根目錄為/usr/local/nginx/html  
 59             root   html;  
 60             #默認訪問文件,歡迎頁先去html目錄下找index.html,如果找不到再去找index.htm  
 61             index  index.html index.htm;  
 62         }  
 63 
 64         #error_page  404              /404.html;  
 65         # redirect server error pages to the static page /50x.html  
 66         #  
 67 
 68         #錯誤頁面及其返回地址,錯誤碼為500、502、503、504都會返回50.html錯誤頁面。  
 69         error_page   500 502 503 504  /50x.html;  
 70         #location後面是"="的話,說明是精確匹配  
 71         location = /50x.html {  
 72             root   html;  
 73         }  
 74 
 75         # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
 76         #  
 77         #location ~ \.php$ {  
 78         #    proxy_pass   http://127.0.0.1;  
 79         #}  
 80 
 81         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
 82         #  
 83         #location ~ \.php$ {  
 84         #    root           html;  
 85         #    fastcgi_pass   127.0.0.1:9000;  
 86         #    fastcgi_index  index.php;  
 87         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
 88         #    include        fastcgi_params;  
 89         #}  
 90 
 91         # deny access to .htaccess files, if Apache‘s document root  
 92         # concurs with nginx‘s one  
 93         #  
 94         #location ~ /\.ht {  
 95         #    deny  all;  
 96         #}  
 97     }  
 98 
 99 
100     # another virtual host using mix of IP-, name-, and port-based configuration  
101     #  
102     #server {  
103     #    listen       8000;  
104     #    listen       somename:8080;  
105     #    server_name  somename  alias  another.alias;  
106 
107     #    location / {  
108     #        root   html;  
109     #        index  index.html index.htm;  
110     #    }  
111     #}  
112 
113 
114     # HTTPS server  
115     #  
116     #server {  
117     #    listen       443 ssl;  
118     #    server_name  localhost;  
119 
120     #    ssl_certificate      cert.pem;  
121     #    ssl_certificate_key  cert.key;  
122 
123     #    ssl_session_cache    shared:SSL:1m;  
124     #    ssl_session_timeout  5m;  
125 
126     #    ssl_ciphers  HIGH:!aNULL:!MD5;  
127     #    ssl_prefer_server_ciphers  on;  
128 
129     #    location / {  
130     #        root   html;  
131     #        index  index.html index.htm;  
132     #    }  
133     #}  
134 
135 }  
136 
137 ---------------------
138 
139 本文來自 PengTDY 的CSDN 博客 ,全文地址請點擊:https://blog.csdn.net/yougoule/article/details/78186138?utm_source=copy 

nginx反向代理服務器以及負載均衡,從安裝到配置