一丐膝、Nginx 常用操作命令和配置文件
1、啟動(dòng)命令
在/usr/local/nginx/sbin 目錄下執(zhí)行 ./nginx
2、關(guān)閉命令
在/usr/local/nginx/sbin 目錄下執(zhí)行 ./nginx -s stop
3慢哈、重新加載命令
在/usr/local/nginx/sbin 目錄下執(zhí)行 ./nginx -s reload
4、nginx.conf 配置文件
nginx 安裝目錄下永票,其默認(rèn)的配置文件都放在這個(gè)目錄的 conf 目錄下岸军,而主配置文件nginx.conf 也在其中,后續(xù)對(duì) nginx 的使用基本上都是對(duì)此配置文件進(jìn)行相應(yīng)的修改瓦侮。
配置文件中有很多#, 開(kāi)頭的表示注釋內(nèi)容佣谐,我們?nèi)サ羲幸?# 開(kāi)頭的段落肚吏,精簡(jiǎn)之后的內(nèi)容如下:
#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 {
? ? 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;
server {
? ? ? ? listen? ? ? 80;
? ? ? ? server_name? localhost;
? ? ? ? #charset koi8-r;
? ? ? ? #access_log? logs/host.access.log? main;
? ? ? ? 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;
? ? #? ? }
? ? #}
}
根據(jù)上述文件,我們可以很明顯的將 nginx.conf 配置文件分為三部分:
第一部分:全局塊
從配置文件開(kāi)始到 events 塊之間的內(nèi)容狭魂,主要會(huì)設(shè)置一些影響 nginx 服務(wù)器整體運(yùn)行的配置指令罚攀,主要包括:配置運(yùn)行 Nginx 服務(wù)器的用戶(hù)(組)、允許生成的 worker process 數(shù)雌澄,進(jìn)程 PID 存放路徑斋泄、日志存放路徑和類(lèi)型以及配置文件的引入等。
比如上面第一行配置的:
#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;
這是 Nginx 服務(wù)器并發(fā)處理服務(wù)的關(guān)鍵配置镐牺,worker_processes 值越大炫掐,可以支持的并發(fā)處理量也越多,但是會(huì)受到硬件睬涧、軟件等設(shè)備的制約募胃。
第二部分:events 塊
比如上面的配置:
events {
? ? worker_connections? 1024;
}
events 塊涉及的指令主要影響 Nginx 服務(wù)器與用戶(hù)的網(wǎng)絡(luò)連接旗唁,常用的設(shè)置包括是否開(kāi)啟對(duì)多 work process 下的網(wǎng)絡(luò)連接進(jìn)行序列化,是否允許同時(shí)接收多個(gè)網(wǎng)絡(luò)連接痹束,選取哪種事件驅(qū)動(dòng)模型來(lái)處理連接請(qǐng)求检疫,每個(gè) word process 可以同時(shí)支持的最大連接數(shù)等。
上述例子就表示每個(gè) work process 支持的最大連接數(shù)為 1024祷嘶。
這部分的配置對(duì) Nginx 的性能影響較大屎媳,在實(shí)際中應(yīng)該靈活配置。
第三部分:http 塊
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;
server {
? ? ? ? listen? ? ? 80;
? ? ? ? server_name? localhost;
? ? ? ? #charset koi8-r;
? ? ? ? #access_log? logs/host.access.log? main;
? ? ? ? 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;
? ? #? ? }
? ? #}
}
這算是 Nginx 服務(wù)器配置中最頻繁的部分论巍,代理烛谊、緩存和日志定義等絕大多數(shù)功能和第三方模塊的配置都在這里。
需要注意的是:http 塊也可以包括 http 全局塊环壤、server 塊晒来。
http 全局塊
http 全局塊配置的指令包括文件引入、MIME-TYPE 定義郑现、日志自定義湃崩、連接超時(shí)時(shí)間、單鏈接請(qǐng)求數(shù)上限等接箫。
server 塊
這塊和虛擬主機(jī)有密切關(guān)系攒读,虛擬主機(jī)從用戶(hù)角度看,和一臺(tái)獨(dú)立的硬件主機(jī)是完全一樣的辛友,該技術(shù)的產(chǎn)生是為了節(jié)省互聯(lián)網(wǎng)服務(wù)器硬件成本薄扁。
每個(gè) http 塊可以包括多個(gè) server 塊,而每個(gè) server 塊就相當(dāng)于一個(gè)虛擬主機(jī)废累。
而每個(gè) server 塊也分為全局 server 塊邓梅,以及可以同時(shí)包含多個(gè) locaton 塊。
**1邑滨、全局 server 塊**
最常見(jiàn)的配置是本虛擬機(jī)主機(jī)的監(jiān)聽(tīng)配置和本虛擬主機(jī)的名稱(chēng)或 IP 配置日缨。
2、location 塊
一個(gè) server 塊可以配置多個(gè) location 塊掖看。
這塊的主要作用是基于 Nginx 服務(wù)器接收到的請(qǐng)求字符串(例如 server_name/uri-string)匣距,對(duì)虛擬主機(jī)名稱(chēng)(也可以是 IP 別名)之外的字符串(例如 前面的 /uri-string)進(jìn)行匹配,對(duì)特定的請(qǐng)求進(jìn)行處理哎壳。地址定向毅待、數(shù)據(jù)緩存和應(yīng)答控制等功能,還有許多第三方模塊的配置也在這里進(jìn)行归榕。
二尸红、Nginx 配置實(shí)例-反向代理
反向代理實(shí)例一
實(shí)現(xiàn)效果:使用 nginx 反向代理,訪問(wèn) www.123.com 直接跳轉(zhuǎn)到 127.0.0.1:8080
具體步驟:
1. 安裝tomcat
解壓縮命令:tar -xvf apache-tomcat-7.0.70.tar.gz
2. 安裝jdk(并配置環(huán)境變量)
首先查看linux 是否自帶jdk
java -version : 如果顯示jdk版本號(hào),則說(shuō)明自帶有jdk驶乾,無(wú)需要額外安裝
如果不顯示邑飒,就需要安裝jdk1.8 并配置環(huán)境變量:
2.1 下載jdk1.8
下載地址 http://www.oracle.com/technetwork/java/javase/downloads/index.html
往下來(lái)拉找到j(luò)dk1.8版本的下載地址,點(diǎn)擊下載對(duì)應(yīng)的tar.gz文件:
將下載的jdk 壓縮包放入 usr/src 下
2.2 解壓jdk壓縮包
命令:tar -zxvf jdk-8u251-linux-x64.tar.gz
2.3 配置jdk環(huán)境變量
鍵入命令 vim /etc/profile 修改配置文件级乐,記得要在root權(quán)限下修改
輸入i進(jìn)入編輯狀態(tài)疙咸,然后將光標(biāo)移到最后一行(GG最后一行 gg第一行),粘貼如下內(nèi)容:
JAVA_HOME=/usr/src/jdk1.8.0_251 ##要根據(jù)自己的解壓目錄設(shè)置
具體內(nèi)容如下:
#java environment
export JAVA_HOME=/usr/src/jdk1.8.0_251
export CLASSPATH=.:${JAVA_HOME}/jre/lib/rt.jar:${JAVA_HOME}/lib/dt.jar:${JAVA_HOME}/lib/tools.jar
export PATH=$PATH:${JAVA_HOME}/bin
效果如下:
然后鍵入命令source /etc/profile 使配置文件生效
2.4 測(cè)試安裝效果
java -version
顯示上圖則jdk 配置完成!
3. 啟動(dòng) tomcat
啟動(dòng)步驟如上圖所示风科,測(cè)試訪問(wèn)(這里需要注意撒轮,如果訪問(wèn)不成功,就有可能是你的linux 防火墻沒(méi)有開(kāi)8080端口贼穆,需要修改下防火墻端口配置):
開(kāi)放端口8080
sudo firewall-cmd --add-port=8080/tcp --permanent
重啟防火墻
firewall-cmd –reload
查看已經(jīng)開(kāi)放的端口
firewall-cmd --list-all
測(cè)試
訪問(wèn)成功题山!
4. 反向代理訪問(wèn)過(guò)程分析
5. 配置反向代理步驟
5.1 本機(jī) host 文件配置
在本機(jī)windows 系統(tǒng)的 host 文件進(jìn)行域名和ip 對(duì)應(yīng)關(guān)系的配置。
在hosts文件最后添加如下配置
192.168.88.132 www.123.com #Linux 主機(jī)ip 本機(jī)配置的域名
瀏覽器地址欄輸入 www.123.com:8080故痊,出現(xiàn)如下界面:
配置完成之后顶瞳,我們便可以通過(guò) www.123.com:8080 訪問(wèn)到第一步出現(xiàn)的 Tomcat 初始界面。那么如何只需要輸入 www.123.com 便可以跳轉(zhuǎn)到 Tomcat 初始界面呢愕秫?便用到 nginx的反向代理慨菱。
5.2 在 nginx 中進(jìn)行請(qǐng)求轉(zhuǎn)發(fā)配置(反向代理配置)
找到nginx 的配置文件 nginx.conf
[root@bogon bin]# cd /usr/local/nginx/
[root@bogon nginx]# ls
client_body_temp? conf? fastcgi_temp? html? logs? proxy_temp? sbin? scgi_temp? uwsgi_temp
[root@bogon nginx]# cd conf/
[root@bogon conf]# ls
fastcgi.conf? ? ? fastcgi_params? ? koi-utf? mime.types? nginx.conf scgi_params? ? ? uwsgi_params? ? ? win-utf? fastcgi.conf.default? fastcgi_params.default? ? koi-win? mime.types.default? nginx.conf.default? scgi_params.default? uwsgi_params.default
[root@bogon conf]# vi nginx.conf
在 nginx.conf 配置文件中增加如下配置:
? ? server {
? ? ? ? listen? ? ? 80;
? ? ? ? server_name? 192.168.88.132; #nginx訪問(wèn)地址
? ? ? ? #charset koi8-r;
? ? ? ? #access_log? logs/host.access.log? main;
? ? ? ? location / {
? ? ? ? ? ? root? html;
? ? ? ? ? ? proxy_pass http://192.168.88.132:8080; #轉(zhuǎn)發(fā)到目標(biāo)地址
? ? ? ? ? ? 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;
? ? ? ? }
如上配置,我們監(jiān)聽(tīng) 80 端口戴甩,訪問(wèn)域名為 www.123.com符喝,不加端口號(hào)時(shí)默認(rèn)為 80 端口,故訪問(wèn)該域名時(shí)會(huì)跳轉(zhuǎn)到 192.168.88.132:8080 路徑上甜孤。
5.3 啟動(dòng)nginx測(cè)試
在瀏覽器端輸入 www.123.com 結(jié)果如下:
注意:修改完nginx.conf 配置文件后协饲,啟動(dòng)nginx 重新加載一下配置文件 ./nginx -s reload
反向代理成功!
反向代理實(shí)例二
實(shí)現(xiàn)效果:使用 nginx 反向代理缴川,根據(jù)訪問(wèn)的路徑跳轉(zhuǎn)到不同端口的服務(wù)中
nginx 監(jiān)聽(tīng)端口為 9001茉稠,
訪問(wèn) http://127.0.0.1:9001/edu/ 直接跳轉(zhuǎn)到 127.0.0.1:8081
訪問(wèn) http://127.0.0.1:9001/vod/ 直接跳轉(zhuǎn)到 127.0.0.1:8082
反向代理配置步驟
1. 準(zhǔn)備兩個(gè) tomcat
在usr/src 下建立兩個(gè)文件夾 tomcat8081 tomcat8082,并在這兩個(gè)文件夾下放入兩個(gè)tomcat 把夸,并解壓而线。
創(chuàng)建文件夾命令:mkdir tomcat8081,mkdir tomcat8082
解壓命令:tar -xvf apache-tomcat-7.0.70.tar.gz
先查看本機(jī)的tomcat是否正在運(yùn)行:ps -ef|grep tomcat扎即,如果正在運(yùn)行根據(jù)運(yùn)行的進(jìn)程id號(hào)殺死正在運(yùn)行的tomcat進(jìn)程 kill 進(jìn)程id號(hào):
修改兩個(gè)tomcat的端口號(hào):一個(gè) 8081 端口,一個(gè) 8082 端口况凉。
進(jìn)入tomcat 的conf文件夾下谚鄙,并vi server.xml,修改內(nèi)容為如下面標(biāo)記位置刁绒。
<?xml version='1.0' encoding='utf-8'?>
<!--
? Licensed to the Apache Software Foundation (ASF) under one or more
? contributor license agreements.? See the NOTICE file distributed with
? this work for additional information regarding copyright ownership.
? The ASF licenses this file to You under the Apache License, Version 2.0
? (the "License"); you may not use this file except in compliance with
? the License.? You may obtain a copy of the License at
? ? ? http://www.apache.org/licenses/LICENSE-2.0
? Unless required by applicable law or agreed to in writing, software
? distributed under the License is distributed on an "AS IS" BASIS,
? WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
? See the License for the specific language governing permissions and
? limitations under the License.
-->
<!-- Note:? A "Server" is not itself a "Container", so you may not
? ? define subcomponents such as "Valves" at this level.
? ? Documentation at /docs/config/server.html
-->
<!-- 為避免沖突從8005改為8025 -->
<Server port="8025" shutdown="SHUTDOWN">
? <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
? <!-- Security listener. Documentation at /docs/config/listeners.html
? <Listener className="org.apache.catalina.security.SecurityListener" />
? -->
? <!--APR library loader. Documentation at /docs/apr.html -->
? <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
? <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
? <Listener className="org.apache.catalina.core.JasperListener" />
? <!-- Prevent memory leaks due to use of particular java/javax APIs-->
? <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
? <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
? <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
? <!-- Global JNDI resources
? ? ? Documentation at /docs/jndi-resources-howto.html
? -->
? <GlobalNamingResources>
? ? <!-- Editable user database that can also be used by
? ? ? ? UserDatabaseRealm to authenticate users
? ? -->
? ? <Resource name="UserDatabase" auth="Container"
? ? ? ? ? ? ? type="org.apache.catalina.UserDatabase"
? ? ? ? ? ? ? description="User database that can be updated and saved"
? ? ? ? ? ? ? factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
? ? ? ? ? ? ? pathname="conf/tomcat-users.xml" />
? </GlobalNamingResources>
? <!-- A "Service" is a collection of one or more "Connectors" that share
? ? ? a single "Container" Note:? A "Service" is not itself a "Container",
? ? ? so you may not define subcomponents such as "Valves" at this level.
? ? ? Documentation at /docs/config/service.html
? -->
? <Service name="Catalina">
? ? <!--The connectors can use a shared executor, you can define one or more named thread pools-->
? ? <!--
? ? <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
? ? ? ? maxThreads="150" minSpareThreads="4"/>
? ? -->
? ? <!-- A "Connector" represents an endpoint by which requests are received
? ? ? ? and responses are returned. Documentation at :
? ? ? ? Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
? ? ? ? Java AJP? Connector: /docs/config/ajp.html
? ? ? ? APR (HTTP/AJP) Connector: /docs/apr.html
? ? ? ? Define a non-SSL HTTP/1.1 Connector on port 8080
? ? -->
? ? <!-- 為避免沖突從8080改為8081 -->
? ? <Connector port="8081" protocol="HTTP/1.1"
? ? ? ? ? ? ? connectionTimeout="20000"
? ? ? ? ? ? ? redirectPort="8443" />
? ? <!-- A "Connector" using the shared thread pool-->
? ? <!--
? ? <Connector executor="tomcatThreadPool"
? ? ? ? ? ? ? port="8080" protocol="HTTP/1.1"
? ? ? ? ? ? ? connectionTimeout="20000"
? ? ? ? ? ? ? redirectPort="8443" />
? ? -->
? ? <!-- Define a SSL HTTP/1.1 Connector on port 8443
? ? ? ? This connector uses the BIO implementation that requires the JSSE
? ? ? ? style configuration. When using the APR/native implementation, the
? ? ? ? OpenSSL style configuration is required as described in the APR/native
? ? ? ? documentation -->
? ? <!--
? ? <Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
? ? ? ? ? ? ? maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
? ? ? ? ? ? ? clientAuth="false" sslProtocol="TLS" />
? ? -->
? ? <!-- Define an AJP 1.3 Connector on port 8009 -->
? <!-- 為避免沖突從8009改為8029 -->
? ? <Connector port="8029" protocol="AJP/1.3" redirectPort="8443" />
? ? <!-- An Engine represents the entry point (within Catalina) that processes
? ? ? ? every request.? The Engine implementation for Tomcat stand alone
? ? ? ? analyzes the HTTP headers included with the request, and passes them
? ? ? ? on to the appropriate Host (virtual host).
? ? ? ? Documentation at /docs/config/engine.html -->
? ? <!-- You should set jvmRoute to support load-balancing via AJP ie :
? ? <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
? ? -->
? ? <Engine name="Catalina" defaultHost="localhost">
? ? ? <!--For clustering, please take a look at documentation at:
? ? ? ? ? /docs/cluster-howto.html? (simple how to)
? ? ? ? ? /docs/config/cluster.html (reference documentation) -->
? ? ? <!--
? ? ? <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
? ? ? -->
? ? ? <!-- Use the LockOutRealm to prevent attempts to guess user passwords
? ? ? ? ? via a brute-force attack -->
? ? ? <Realm className="org.apache.catalina.realm.LockOutRealm">
? ? ? ? <!-- This Realm uses the UserDatabase configured in the global JNDI
? ? ? ? ? ? resources under the key "UserDatabase".? Any edits
? ? ? ? ? ? that are performed against this UserDatabase are immediately
? ? ? ? ? ? available for use by the Realm.? -->
? ? ? ? <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
? ? ? ? ? ? ? resourceName="UserDatabase"/>
? ? ? </Realm>
? ? ? <Host name="localhost"? appBase="webapps"
? ? ? ? ? ? unpackWARs="true" autoDeploy="true">
? ? ? ? <!-- SingleSignOn valve, share authentication between web applications
? ? ? ? ? ? Documentation at: /docs/config/valve.html -->
? ? ? ? <!--
? ? ? ? <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
? ? ? ? -->
? ? ? ? <!-- Access log processes all example.
? ? ? ? ? ? Documentation at: /docs/config/valve.html
? ? ? ? ? ? Note: The pattern used is equivalent to using pattern="common" -->
? ? ? ? <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
? ? ? ? ? ? ? prefix="localhost_access_log." suffix=".txt"
? ? ? ? ? ? ? pattern="%h %l %u %t "%r" %s %b" />
? ? ? </Host>
? ? </Engine>
? </Service>
</Server>
修改完成后闷营,到bin目錄下 ./startup.sh 運(yùn)行8082端口的tomcat
同理8082端口的tomcat 同樣如上配置并運(yùn)行啟動(dòng)!
2.為兩個(gè)tomcat 添加頁(yè)面
向兩個(gè)tomcat服務(wù)器的webapps下分別創(chuàng)建edu 和 文件夾,并將a.html 和 b.html 分別放入這兩個(gè)文件夾傻盟。(注:a.html 和 b.html 自己隨便建立一個(gè)速蕊,里面輸入自己想展示的信息,能夠區(qū)分是來(lái)自不同tomcat 服務(wù)器即可)
測(cè)試訪問(wèn):
3. nginx.conf配置反向代理
#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 {
? ? 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;
? ? server {
? ? ? ? listen? ? ? 80;
? ? ? ? server_name? 192.168.88.132;
? ? ? ? #charset koi8-r;
? ? ? ? #access_log? logs/host.access.log? main;
? ? ? ? location / {
? ? ? ? ? ? root? html;
? ? ? ? ? ? proxy_pass http://127.0.0.1:8080;
? ? ? ? ? ? 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
? ? #
? ? ? ? #? ? 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
? ? # 在這里把注釋去掉娘赴,添加一個(gè)server 端口監(jiān)聽(tīng)9001
? ? server {
? ? ? ? listen? ? ? 9001;
? ? ? ? server_name? 192.168.88.132;
# 在下面 配置轉(zhuǎn)發(fā)到不同的tomcat 中的內(nèi)容 格式:~ /項(xiàng)目名/
? ? ? ? location ~ /edu/ {
proxy_pass http://127.0.0.1:8001;
? ? ? ? }
? ? ? ? location ~ /vod/ {
proxy_pass http://127.0.0.1:8002;
? ? ? ? }
? ? }
? ? # 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;
? ? #? ? }
? ? #}
}
配置完成后规哲,開(kāi)放對(duì)外訪問(wèn)的端口號(hào),如果防火墻已經(jīng)關(guān)閉就不用了诽表。
在/nginx/sbin/中執(zhí)行 ./nginx -s reload 重新加載配置文件唉锌。
4. 測(cè)試
5. location 指令說(shuō)明
該指令用于匹配 URL。
語(yǔ)法如下:
= :用于不含正則表達(dá)式的 uri 前竿奏,要求請(qǐng)求字符串與 uri 嚴(yán)格匹配袄简,如果匹配
成功,就停止繼續(xù)向下搜索并立即處理該請(qǐng)求泛啸。
~:用于表示 uri 包含正則表達(dá)式绿语,并且區(qū)分大小寫(xiě)。
~*:用于表示 uri 包含正則表達(dá)式候址,并且不區(qū)分大小寫(xiě)吕粹。
^~:用于不含正則表達(dá)式的 uri 前,要求 Nginx 服務(wù)器找到標(biāo)識(shí) uri 和請(qǐng)求字符串匹配度最高的 location 后宗雇,立即使用此 location 處理請(qǐng)求昂芜,而不再使用 location 塊中的正則 uri 和請(qǐng)求字符串做匹配。
注意:如果 uri 包含正則表達(dá)式赔蒲,則必須要有 ~ 或者 ~* 標(biāo)識(shí)泌神。
三、Nginx 配置實(shí)例-負(fù)載均衡
1舞虱、實(shí)現(xiàn)效果
瀏覽器地址欄輸入地址http://192.168.17.129/edu/a.html欢际,負(fù)載均衡效果,平均到8082和 8081 端口中矾兜。
2损趋、準(zhǔn)備工作
準(zhǔn)備兩 tomcat 服務(wù)器,一臺(tái) 8082椅寺,一臺(tái) 8081
在兩臺(tái) tomcat 里面 webapps 目錄中浑槽,創(chuàng)建名稱(chēng)是 edu 文件夾,在edu 文件夾中創(chuàng)建頁(yè)面 a.html返帕,用于測(cè)試桐玻。
3. 在 nginx 的配置文件中進(jìn)行負(fù)載均衡的配置
nginx.conf配置如下:
#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 {
? ? 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;
? ? # 加上upstream
? ? upstream myserver {
? ? ? ? server? 192.168.88.132:8080; #參與負(fù)載均衡的服務(wù)器地址1
? ? ? ? server? 192.168.88.132:8081; #參與負(fù)載均衡的服務(wù)器地址2
? ? }
? ? ? ? server {
? ? ? ? listen? ? ? 80;
? ? ? ? server_name? 192.168.88.132;
? ? ? ? #charset koi8-r;
? ? ? ? #access_log? logs/host.access.log? main;
? ? ? ? location / {
? ? ? ? # 加上代理規(guī)則
? ? ? ? ? ? proxy_pass http://myserver;
? ? ? ? ? ? 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
? ? # 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;
? ? #? ? }
? ? #}
}
4.訪問(wèn)測(cè)試
訪問(wèn)http://192.168.88.132/edu/a.html:第一次訪問(wèn)顯示如下:
刷新頁(yè)面,效果如下:
可以看出荆萤,訪問(wèn)該頁(yè)面的時(shí)候镊靴,nginx會(huì)把請(qǐng)求平均分擔(dān)到8080和8081兩個(gè)服務(wù)器铣卡。
5.nginx分配服務(wù)器策略
第一種 輪詢(xún)策略(默認(rèn)):
每個(gè)請(qǐng)求按時(shí)間順序逐一分配到不同的后端服務(wù)器,如果后端服務(wù)器 down 掉偏竟,能自動(dòng)剔除煮落。
第二種 權(quán)重策略 weight:
weight 代表權(quán)重默認(rèn)為 1,權(quán)重越高被分配的客戶(hù)端越多。
#例如:
? ? upstream myserver {
? ? ? ? server? 192.168.88.132:8080 weight=5; #參與負(fù)載均衡的服務(wù)器地址1
? ? ? ? server? 192.168.88.132:8081 weight=10; #參與負(fù)載均衡的服務(wù)器地址2
? ? }
# 如上踊谋,8080的權(quán)重為5 8081權(quán)重為10蝉仇,則nginx 負(fù)載均衡分配時(shí),分配的8081比例更大
第三種 ip哈希策略 ip_hash:
每個(gè)請(qǐng)求按訪問(wèn) ip 的hash 結(jié)果分配褪子,這樣每個(gè)訪客固定訪問(wèn)一個(gè)后端服務(wù)器量淌,可以解決session問(wèn)題。
#例如:
? ? upstream myserver {
? ? ip_hash
? ? ? ? server? 192.168.88.132:8080; #參與負(fù)載均衡的服務(wù)器地址1
? ? ? ? server? 192.168.88.132:8081; #參與負(fù)載均衡的服務(wù)器地址2
? ? }
# 如上嫌褪,每個(gè)請(qǐng)求按訪問(wèn) ip 的hash 結(jié)果分配呀枢,即:某一個(gè)用戶(hù)訪問(wèn)該服務(wù)器,根據(jù)該用戶(hù)的ip地址分配一個(gè)ip_hash笼痛,并隨機(jī)分配到端口為8080或者8081的服務(wù)器裙秋,該用戶(hù)這個(gè)ip下次再訪問(wèn)服務(wù)器時(shí),仍然默認(rèn)訪問(wèn)上次分配的服務(wù)器缨伊,而不會(huì)再次隨機(jī)分配摘刑。
第四種 fair(第三方):
按后端服務(wù)器的響應(yīng)時(shí)間來(lái)分配請(qǐng)求,響應(yīng)時(shí)間短的優(yōu)先分配刻坊。
#例如:
? ? upstream myserver {
? ? ? ? server? 192.168.88.132:8080; #參與負(fù)載均衡的服務(wù)器地址1
? ? ? ? server? 192.168.88.132:8081; #參與負(fù)載均衡的服務(wù)器地址2
? ? ? ? fair
? ? }
# 如上枷恕,訪問(wèn)該服務(wù)器時(shí),通過(guò)判斷 8080 或者 8081 服務(wù)器響應(yīng)的時(shí)間谭胚,來(lái)進(jìn)行分配訪問(wèn)徐块,誰(shuí)的響應(yīng)時(shí)間短,優(yōu)先訪問(wèn)誰(shuí)灾而。
四胡控、Nginx配置實(shí)例-動(dòng)靜分離
1. 動(dòng)靜分離簡(jiǎn)述
Nginx 動(dòng)靜分離簡(jiǎn)單來(lái)說(shuō)就是把動(dòng)態(tài)跟靜態(tài)請(qǐng)求分開(kāi),不能理解成只是單純的把動(dòng)態(tài)頁(yè)面和靜態(tài)頁(yè)面物理分離旁趟。嚴(yán)格意義上說(shuō)應(yīng)該是動(dòng)態(tài)請(qǐng)求跟靜態(tài)請(qǐng)求分開(kāi)昼激,可以理解成使用 Nginx 處理靜態(tài)頁(yè)面,Tomcat 處理動(dòng)態(tài)頁(yè)面锡搜。動(dòng)靜分離從目前實(shí)現(xiàn)角度來(lái)講大致分為兩種橙困,一種是純粹把靜態(tài)文件獨(dú)立成單獨(dú)的域名,放在獨(dú)立的服務(wù)器上耕餐,也是目前主流推崇的方案凡傅;另外一種方法就是動(dòng)態(tài)跟靜態(tài)文件混合在一起發(fā)布,通過(guò) nginx 來(lái)分開(kāi)蛾方。
通過(guò)location 指定不同的后綴名實(shí)現(xiàn)不同的請(qǐng)求轉(zhuǎn)發(fā)像捶。通過(guò) expires 參數(shù)設(shè)置,可以使瀏覽器緩存過(guò)期時(shí)間桩砰,減少與服務(wù)器之前的請(qǐng)求和流量拓春。具體 Expires 定義:是給一個(gè)資源設(shè)定一個(gè)過(guò)期時(shí)間,也就是說(shuō)無(wú)需去服務(wù)端驗(yàn)證亚隅,直接通過(guò)瀏覽器自身確認(rèn)是否過(guò)期即可硼莽,所以不會(huì)產(chǎn)生額外的流量。此種方法非常適合不經(jīng)常變動(dòng)的資源煮纵。(如果經(jīng)常更新的文件懂鸵,不建議使用 Expires 來(lái)緩存),我這里設(shè)置 3d行疏,表示在這 3 天之內(nèi)訪問(wèn)這個(gè) URL匆光,發(fā)送一個(gè)請(qǐng)求,比對(duì)服務(wù)器該文件最后更新時(shí)間沒(méi)有變化酿联,則不會(huì)從服務(wù)器抓取终息,返回狀態(tài)碼304,如果有修改贞让,則直接從服務(wù)器重新下載周崭,返回狀態(tài)碼 200。
2. 準(zhǔn)備工作
在 liunx 系統(tǒng)中準(zhǔn)備靜態(tài)資源喳张,用于進(jìn)行訪問(wèn):
首先在根路徑下創(chuàng)建 staticresource 文件夾续镇,并在其中建立 www 和 image文件夾用于存放靜態(tài)資源:
在www 文件夾中放置一個(gè)a.html 在 image 放置lf.jpg
3. 具體配置
在nginx 配置文件中配置靜態(tài)資源訪問(wèn)
配置內(nèi)容如下:
#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 {
? ? 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;
? ? ? ? server {
? ? ? ? listen? ? ? 80;
? ? ? ? server_name? 192.168.88.132;
? ? ? ? #charset koi8-r;
? ? ? ? #access_log? logs/host.access.log? main;
#配置靜態(tài)資源訪問(wèn)
? ? ? ? location /www/ {
? ? ? ? ? ? root? /staticresource/;
? ? ? ? ? ? index? index.html index.htm;
? ? ? ? }
? ? ? ? location /image/{
? ? ? ? ? ? root /staticresource/;
? ? ? ? ? ? autoindex on;
? ? ? ? }
? ? ? ? #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;
? ? ? ? #}
}
? ? # 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;
? ? #? ? }
? ? #}
}
4. 測(cè)試查看效果
瀏覽器中輸入地址
http://192.168.88.132/image/lf.jpg
http://192.168.88.132/www/a.html
Nginx配置高可用的集群(了解)
什么是高可用
1、需要的環(huán)境
(1)需要兩臺(tái) nginx 服務(wù)器
(2)需要 keepalived
(3)需要虛擬 ip
2销部、配置高可用的準(zhǔn)備工作
(1)需要兩臺(tái)服務(wù)器 192.168.17.129 和 192.168.17.131
(2)在兩臺(tái)服務(wù)器安裝 nginx
(3)在兩臺(tái)服務(wù)器安裝 keepalived
3摸航、在兩臺(tái)服務(wù)器安裝 keepalived
(1)使用 yum 命令進(jìn)行安裝
yum install keepalived –y
(2)安裝之后,在 etc 里面生成目錄 keepalived柴墩,有文件 keepalived.conf
4忙厌、完成高可用配置(主從配置)
(1)修改/etc/keepalived/keepalivec.conf 配置文件
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.17.129
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "/usr/local/src/nginx_check.sh"
interval 2 #(檢測(cè)腳本執(zhí)行的間隔)
weight 2
}
vrrp_instance VI_1 {
state BACKUP # 備份服務(wù)器上將 MASTER 改為 BACKUP
interface ens33 //網(wǎng)卡
virtual_router_id 51 # 主、備機(jī)的 virtual_router_id 必須相同
priority 90 # 主江咳、備機(jī)取不同的優(yōu)先級(jí)逢净,主機(jī)值較大,備份機(jī)值較小
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.17.50 // VRRP H 虛擬地址
}
}
(2)在/usr/local/src 添加新建檢測(cè)腳本 nginx_check.sh
#! /bin/bash
A=`ps -C nginx -no-header | wc - 1`
if [ $A -eq 0];then
????????/usr/local/nginx/sbin/nginx
????????sleep 2
????????if [`ps -C nginx --no-header| wc -1` -eq 0 ];then
????????????????killall keepalived
????????fi
fi
(3)把兩臺(tái)服務(wù)器上 nginx 和 keepalived 啟動(dòng)
啟動(dòng) nginx:./nginx
啟動(dòng) keepalived:systemctl start keepalived.service
$systemctl start keepalived.service #keepalived啟動(dòng)
$ps -ef I grep keepalived #查看keepalived是否啟動(dòng)
5歼指、最終測(cè)試
(1)在瀏覽器地址欄輸入 虛擬 ip 地址 192.168.17.50
(2)把主服務(wù)器(192.168.17.129)nginx 和 keepalived 停止爹土,再輸入 192.168.17.50
$systemctl stop keepalived.service#keepalived停止
Nginx的原理
1、mater 和 worker
2踩身、worker如何進(jìn)行工作的
3胀茵、一個(gè) master 和多個(gè) woker 有好處
(1)可以使用 nginx –s reload 熱部署棺蛛,利用 nginx 進(jìn)行熱部署操作
(2)每個(gè) woker 是獨(dú)立的進(jìn)程正罢,如果有其中的一個(gè) woker 出現(xiàn)問(wèn)題,其他 woker 獨(dú)立的攀芯,
繼續(xù)進(jìn)行爭(zhēng)搶?zhuān)瑢?shí)現(xiàn)請(qǐng)求過(guò)程,不會(huì)造成服務(wù)中斷
4脱拼、設(shè)置多少個(gè) woker 合適
Nginx同redis類(lèi)似都采用了io多路復(fù)用機(jī)制瞒瘸,每個(gè)worker都是一個(gè)獨(dú)立的進(jìn)程, 但每個(gè)進(jìn)
程里只有一個(gè)主線(xiàn)程熄浓,通過(guò)異步非阻塞的方式來(lái)處理請(qǐng)求情臭,即使是 千上萬(wàn)個(gè)請(qǐng)求也不在話(huà)
下。每個(gè)worker的線(xiàn)程可以把一個(gè)cpu的性能發(fā)揮到極致赌蔑。所以worker數(shù)和服務(wù)器的cpu
數(shù)相等是最為適宜的俯在。設(shè)少了會(huì)浪費(fèi)cpu,設(shè)多了會(huì)造成cpu頻繁切換上下文帶來(lái)的損耗。
# 設(shè)置worker數(shù)量
worker.processes 4
# work綁定cpu(4work綁定4cpu)
worker_cpu_affinity 0001 0010 0100 1000
# work綁定cpu (4work綁定8cpu中的4個(gè))
worker_cpu_affinity 0000001 00000010 00000100 00001000
5娃惯、連接數(shù) worker_connection
這個(gè)值是表示每個(gè)worker進(jìn)程所能建立連接的最大值跷乐,所以,一個(gè)nginx 能建立的最大連接數(shù)趾浅,應(yīng)該是worker.connections * worker processes劈猿。當(dāng)然,這里說(shuō)的是最大連接數(shù)潮孽,對(duì)于HTTP 請(qǐng)求本地資源來(lái)說(shuō)揪荣,能夠支持的最大并發(fā)數(shù)量是worker.connections * worker processes,如果是支持http1.1的瀏覽器每次訪問(wèn)要占兩個(gè)連接,所以普通的靜態(tài)訪問(wèn)最大并發(fā)數(shù)是: worker.connections * worker.processes / 2, 而如果是HTTP作為反向代理來(lái)說(shuō)往史,最大并發(fā)數(shù)量應(yīng)該是worker.connections * worker_proceses/4. 因?yàn)樽鳛榉聪虼矸?wù)器仗颈,每個(gè)并發(fā)會(huì)建立與客戶(hù)端的連接和與后端服務(wù)的連接,會(huì)占用兩個(gè)連接.
第一個(gè): 發(fā)送請(qǐng)求椎例,占用了woker的幾個(gè)連接數(shù)?
答案: 2或者4個(gè)挨决。
第二個(gè): nginx有一個(gè)master,有四個(gè)woker,每個(gè)woker支持最大的連接數(shù)1024,支持的最大并發(fā)數(shù)是多少?
答案:普通的靜態(tài)訪問(wèn)最大并發(fā)數(shù)是: worker connections * worker processes /2,
而如果是HTTP作為反向代理來(lái)說(shuō)订歪,最大并發(fā)數(shù)量應(yīng)該是worker connections * worker processes/4