Nginx (engine x)是一個高性能的HTTP和反向代理服務器吼砂,也是一個IMAP/POP3/SMTP服務器留荔。Nginx可以在大多數(shù) UnixLinux OS 上編譯運行,并有 Windows 移植版怜奖。
代理服務器:一般是指局域網(wǎng)內部的機器通過代理服務器發(fā)送請求到互聯(lián)網(wǎng)上的服務器,代理服務器一般作用在客戶端妖胀。
反向代理(Reverse Proxy)方式是指以代理服務器來接受internet上的連接請求,然后將請求轉發(fā)給內部網(wǎng)絡上的服務器档礁,并將從服務器上得到的結果返回給internet上請求連接的客戶端角钩,此時代理服務器對外就表現(xiàn)為一個服務器。
這里講得很直白呻澜。反向代理方式實際上就是一臺負責轉發(fā)的代理服務器递礼,貌似充當了真正服務器的功能,但實際上并不是羹幸,代理服務器只是充當了轉發(fā)的作用脊髓,并且從真正的服務器那里取得返回的數(shù)據(jù)。
負載均衡的作用:
例如一臺服務器本身的性能能夠支持1w個業(yè)務并發(fā)處理
如果業(yè)務并發(fā)少于1w個栅受,機器也能負重前行
但是如果有5w個怎么辦呢
簡單的辦法是使用nginx做前面的類似于堡壘機
10w個并發(fā)都打到這個nginx
但是nginx本身不處理業(yè)務将硝,所以他能接納10w個并發(fā)恭朗,但是他本身沒有處理能力
而是把這個10w個并發(fā),按照一定的策略分配給后面的其他機器
這樣的好處是
1.通過1個nginx+n個后面的機器組成一個小集群袋哼,能處理超過單臺機器接納的上線
2.對外提供服務一般會固定一個ip冀墨,而往往你的業(yè)務一個ip(也就是單臺服務器)不能完全處理,那么找一個性能不錯的專門負責轉發(fā)外面的業(yè)務請求(一般這種對服務器的壓力不會太大)涛贯,然后發(fā)給真正需要處理業(yè)務的后面的例如tomcat去诽嘉,這樣能夠對外提供一致的提供服務的點
如果并發(fā)有50w個怎么辦?
這種即使僅僅是轉發(fā)弟翘,單個nginx都沒法處理虫腋,性能要求太高,而且即使性能能夠跟上稀余,操作系統(tǒng)的端口數(shù)也是有限的
這個時候就需要高級的轉發(fā)服務器了
這方面有硬件的例如f5或者軟件的ha
然后將請求分發(fā)給nginx悦冀,nginx再分發(fā)給具體處理業(yè)務的例如tomcat
但是功能都是一樣的:提供統(tǒng)一的對外訪問入口,轉發(fā)請求給真正的執(zhí)行者
安裝步驟:
1睛琳,先安裝gcc 等
yum -y install gcc gcc-c++ wget
2盒蟆,安裝一些庫
yum -y install gcc wget automake autoconf libtool libxml2-devel libxslt-devel perl-devel perl-ExtUtils-Embed pcre-devel openssl-devel
3,進入默認的軟件目錄
cd /usr/local/src/
4师骗,下載nginx軟件(進入下載頁面查看最新版本http://nginx.org/download)
wget http://nginx.org/download/nginx-1.13.3.tar.gz
5历等,解壓
tar zxvf nginx-1.13.3.tar.gz
6,進入 nginx1.13.3的源碼 ?如果想改版本號 可以進入源碼目錄src/core/nginx.h更改
cd nginx-1.13.3/
7辟癌,創(chuàng)建一個nginx目錄用來存放運行的臨時文件夾
mkdir -p /var/cache/nginx
8寒屯,開始configure
./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--user=nobody \
--group=nobody \
--with-pcre \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-mail \
--with-mail_ssl_module \
--with-file-aio \
--with-ipv6 \
--with-http_v2_module \
--with-threads \
--with-stream \
--with-stream_ssl_module
9,編譯
make
10黍少,安裝
make install
11寡夹,啟動nginx
/usr/sbin/nginx
12,配置服務
vi /usr/lib/systemd/system/nginx.service
輸入以下內容后保存
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
文件保存后厂置,剛剛配置的服務需要讓systemctl能識別菩掏,就必須刷新配置
systemctl daemon-reload
13,設置開機啟動
systemctl enable nginx.service
重啟電腦昵济,看到nginx已自啟患蹂。
reboot
ps -ef|grep nginx
14,后面可以用systemctl來操作nginx.service
systemctl start nginx.service
systemctl stop nginx.service
如果使用命令出現(xiàn)如下報錯
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
修改文件/usr/lib/systemd/system/nginx.service測試是否能啟用服務砸紊。
vi /usr/lib/systemd/system/nginx.service
輸入以下內容后保存
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx
ExecStart=/usr/sbin/nginx
ExecStop=/bin/kill nginx
PrivateTmp=true
[Install]
WantedBy=multi-user.target
15,查看nginx版本
nginx -v
訪問nginx囱挑,現(xiàn)在你可以通過公網(wǎng)ip (本地可以通過 localhost /或 127.0.0.1 ) 查看nginx 服務返回的信息醉顽。
curl -i localhost
16,17平挑,18是修改nginx配置文件的例子游添,配置文件位于/usr/local/src/nginx-1.13.3/conf/
使用命令
vi /usr/local/src/nginx-1.13.3/conf/nginx.conf
16系草,常見例子->訪問靜態(tài)文件
#定義Nginx運行的用戶和用戶組
user ?nginx nginx;
#nginx進程數(shù),建議設置為等于CPU總核心數(shù)唆涝。用lscpu命令查看cou核數(shù)
worker_processes ?1;
#nginx默認是沒有開啟利用多核cpu的配置的找都。需要通過增加worker_cpu_affinity配置參數(shù)來充分利用多核cpu。
#worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
#一個nginx進程打開的最多文件描述符數(shù)目廊酣,理論值應該是最多打開文件數(shù)(系統(tǒng)的值ulimit -n)與nginx進程數(shù)相除能耻,但是nginx分配請求并不均勻,所以建議與ulimit -n的值保持一致亡驰。
worker_rlimit_nofile 65536;
#全局錯誤日志定義類型晓猛,[ debug | info | notice | warn | error | crit ]
#error_log ?logs/error.log;
#error_log ?logs/error.log ?notice;
#error_log ?logs/error.log ?info;
#進程文件
#pid ??/var/run/nginx.pid;
events {
#單個進程最大連接數(shù)(最大連接數(shù)=連接數(shù)*進程數(shù))
????worker_connections ?65536;
}
#設定http服務器
http {
#文件擴展名與文件類型映射表
????include ??????mime.types;
#默認文件類型
default_type ?application/octet-stream;
????log_format ?main ?'$remote_addr [$time_local] $upstream_addr $upstream_status $upstream_response_time "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"';
????log_format cachelog '$time_local - $upstream_cache_status - Cache-Control:$upstream_http_cache_control - $request($status) - ';
????access_log ?logs/access.log ?main;
????sendfile ???????on;
????tcp_nopush ????on;
????proxy_ignore_client_abort on;
#長連接超時時間,單位是秒
????keepalive_timeout ?65;
#默認編碼
????charset utf-8;
#gzip模塊設置
????gzip on; #開啟gzip壓縮輸出
????gzip_min_length 10k; #最小壓縮文件大小
????gzip_buffers 4 16k; #壓縮緩沖區(qū)
????gzip_comp_level 2; #壓縮等級
#壓縮類型凡辱,默認就已經(jīng)包含text/html戒职,所以下面就不用再寫了,寫上去也不會有問題透乾,但是會有一個warn洪燥。
????gzip_types text/plain text/javascript application/javascript application/x-javascript text/css ?application/xml application/octet-stream;
????gzip_vary on;
#虛擬主機的配置
????server {
#被監(jiān)聽的端口號和網(wǎng)址
????????listen ??????80;
#域名可以有多個,用空格隔開
????????server_name ?www.test.com;
????????#charset koi8-r;
#定義本虛擬主機的訪問日志
????????access_log ?logs/test_access.log ?main;
#對 "/" 啟用反向代理
????????location / {
#這個地方指定被訪問的文件夾位置
????????????root ??/data/test;
????????????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;
????????}
#圖片緩存時間設置
????????location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css|js)$ {
????????root /data/test;
????????expires 30d;
????????}
????}
????#location ~ /purge(/.*){
????# ???allow 192.168.0.0/16;
????# ???deny all;
????# ???proxy_cache_purge resource $host$1$is_args$args;
????#}
}
17乳乌,常見例子->一個負載均衡例子
user ?nginx nginx;
worker_processes ?8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
worker_rlimit_nofile 65536;
events {
????worker_connections ?65536;
}
http {
????include ??????mime.types;
????default_type ?application/octet-stream;
# log_format日志格式
????log_format ?main ?'$remote_addr [$time_local] $upstream_addr $upstream_status $upstream_response_time "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"';
log_format cachelog '$time_local - $upstream_cache_status - Cache-Control:$upstream_http_cache_control - $request($status) - ';
????access_log ?logs/access.log ?main;
????sendfile ???????on;
????tcp_nopush ????on;
????proxy_ignore_client_abort on;
????keepalive_timeout ?65;
????#keepalive_timeout ?1000;
????charset utf-8;
????gzip on;
????gzip_min_length 10k;
????gzip_buffers 4 16k;
????gzip_comp_level 2;
????gzip_types text/plain text/javascript application/javascript application/x-javascript text/css ?application/xml application/octet-stream;
????gzip_vary on;
????upstream balance{
????????server 192.168.21.77:8080;
????}
????server {
????????listen ??????80;
????????server_name www.website.com;
????????#charset koi8-r;
????????access_log ?logs/website.log ?main;
????#request proxy server
????????location / {
????????proxy_set_header Host $host;
????????proxy_set_header X-Real-IP $remote_addr;
????????proxy_set_header REMOTE-HOST $remote_addr;
#后端的Web服務器可以通過X-Forwarded-For獲取用戶真實IP
????????proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#這里設置代理的位置
????????????proxy_pass http://balance;
????????????proxy_redirect default;
????????}
????????#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;
????????}
#設定查看Nginx狀態(tài)的地址
????location /nginx_status{
????????????????stub_status on;
access_log off;#指定全局的 log 是否打開
????????????????allow all;
???????????????# deny all;
????????}
????}
}
18捧韵,常見例子->多個負載均衡,多個服務钦扭,多個端口
user ?nginx nginx;
worker_processes ?8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
worker_rlimit_nofile 65536;
#error_log ?logs/error.log;
#error_log ?logs/error.log ?notice;
#error_log ?logs/error.log ?info;
#pid ???????logs/nginx.pid;
events {
????worker_connections ?65536;
}
http {
????include ??????mime.types;
????default_type ?application/octet-stream;
????log_format ?main ?'$remote_addr [$time_local] $upstream_addr $upstream_status $upstream_response_time "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"';
????log_format cachelog '$time_local - $upstream_cache_status - Cache-Control:$upstream_http_cache_control - $request($status) - ';
#全局日志記錄記錄的位置及日志格式
????access_log ?logs/access.log ?main;
????sendfile ???????on;
????tcp_nopush ????on;
????proxy_ignore_client_abort on;
????keepalive_timeout ?65;
????#keepalive_timeout ?1000;
????charset utf-8;
????gzip on;
????gzip_min_length 10k;
????gzip_buffers 4 16k;
????gzip_comp_level 2;
????gzip_types text/plain text/javascript application/javascript application/x-javascript text/css ?application/xml application/octet-stream;
????gzip_vary on;
#負載均衡示例 1
????upstream balance1{
????????server 192.168.21.76:8093 max_fails=3 fail_timeout=30s;
????}
#負載均衡示例 2
????upstream balance2{
????????server 192.168.21.76:8070;
????????server 192.168.21.76:8071 down;
????}
#負載均衡示例 3
????upstream balance3{
????????server 192.168.21.76:8080 max_fails=3 fail_timeout=30s;
????}
#web服務 1
????server {
????????listen ??????80;
????????server_name www.website1.com;
????????#charset koi8-r;
#當前 web 服務的日志 位置纫版、格式
????????access_log ?logs/404_access.log ?main;
????#request proxy server
????????location / {
????????proxy_set_header Host $host;
????????proxy_set_header X-Real-IP $remote_addr;
????????proxy_set_header REMOTE-HOST $remote_addr;
????????proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
????????????proxy_pass http://balance1;
????????????proxy_redirect default;
????????}
????????#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;
????????}
????location /nginx_status{
????????????????stub_status on;
access_log off;#指定全局的 access_log 是否打開
????????????????allow all;
???????????????# deny all;
????????}
????}
????server {
????????listen ??????80;
????????server_name website2.com;
????????#charset koi8-r;
????????access_log ?logs/website2.log ?main;
????#request proxy server
????????location / {
????????proxy_set_header Host $host;
????????proxy_set_header X-Real-IP $remote_addr;
????????proxy_set_header REMOTE-HOST $remote_addr;
????????proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
????????????proxy_pass http://balance3;
????????????proxy_redirect default;
????????}
????????#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;
????????}
????location /nginx_status{
????????????????stub_status on;
????????????????access_log off;
????????????????allow all;
???????????????# deny all;
????????}
????}
????server {
????????listen ??????80;
????????server_name www.website3.com;
????????#charset koi8-r;
????????access_log ?logs/website3.log ?main;
????#request proxy server
????????location / {
????????proxy_set_header Host $host;
????????proxy_set_header X-Real-IP $remote_addr;
????????proxy_set_header REMOTE-HOST $remote_addr;
????????proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
????????????proxy_pass http://balance1;
????????????proxy_redirect default;
????????}
????????#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;
????????}
????location /nginx_status{
????????????????stub_status on;
????????????????access_log off;
????????????????allow all;
???????????????# deny all;
????????}
}
????server {
????????listen ??????8060;
????????server_name 192.168.1.111;
????????#charset koi8-r;
????????access_log ?logs/website4.log ?main;
????#request proxy server
????????location / {
????????proxy_set_header Host $host;
????????proxy_set_header X-Real-IP $remote_addr;
????????proxy_set_header REMOTE-HOST $remote_addr;
????????proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
????????????proxy_pass http://balance2;
????????????proxy_redirect default;
????????}
????????#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;
????????}
????location /nginx_status{
????????????????stub_status on;
????????????????access_log off;
????????????????allow all;
???????????????# deny all;
????????}
????}
}