nginx-1.14.0使用
簡単にwindowsでご紹介します
nginx
steamモジュール転送tcpはsocketに負荷等化を行う
次のように構成されています.
upstreamモジュールはweb socketを転送して負荷均衡を行う
次のように構成されています.
共通の構成
nginxNginxとは高性能のhttpサーバ/リバースエージェントサーバおよびEメール(IMAP/POP 3)エージェントサーバである.ロシアのプログラマーIgor Sysoevが開発したもので、公式テストではnginxが5万同時リンクをサポートし、cpu、メモリなどのリソース消費が非常に低く、動作が非常に安定している.
シーン1、httpサーバを適用します.Nginxはhttpサービスであり、httpサービスを独立して提供することができる.Web静的サーバを作成できます.2、仮想ホスト.1台のサーバで複数のWebサイトを仮想化できます.たとえば、パーソナルWebサイトで使用される仮想ホスト.3、リバースエージェント、負荷バランス.Webサイトへのアクセスがある程度になると、単一のサーバがユーザの要求を満たすことができない場合、nginxを逆エージェントとして複数のサーバクラスタを使用する必要があります.また、複数のサーバは、負荷を平均的に分担することができ、あるサーバの負荷が高いため、あるサーバがアイドル状態になることはありません.
nginxインストールダウンロードnginx:
要求される実装環境1、gccを実装する必要がある環境.yum install gcc-c++2、サードパーティの開発パッケージ.PCREPCRE(Perl Compatible Regular Expressions)は、perl互換性のある正規表現ライブラリを含むPerlライブラリです.nginxのhttpモジュールはpcreを使用して正規表現を解析するので、linuxにpcreライブラリをインストールする必要があります.yum install-y pcre pcre-devel注:pcre-develはpcreを使用して開発された二次開発ライブラリです.nginxにもこのライブラリが必要です.zlibzlibライブラリは圧縮と解凍の方法を多く提供しており、nginxはzlibを使用してhttpパッケージの内容をgzipするため、linuxにzlibライブラリをインストールする必要があります.yum install -y zlib zlib-devel
インストール手順
nginxを起動してsbinディレクトリに入る[root@localhost sbin]# ./nginxを閉じるnginx:[root@localhost sbin]# ./nginx-s stop推奨使用:[root@localhost sbin]# ./nginx -s quit
仮想ホストの構成は、1台のサーバで複数のWebサイトを起動することです.異なるウェブサイトを区別する方法:1、ドメイン名が異なる2、ポートが異なる
ポートによって異なる仮想マシンNginxのプロファイルを区別する:/usr/local/nginx/conf/nginx.conf
ドメイン名で仮想ホストとドメイン名を区別するのがウェブサイトです.www.baidu.comwww.taobao.comwww.jd.comTcp/ip
リバースエージェントリバースエージェントサーバは、どのサーバがサービスを提供しているかを決定し、プロキシサーバがサーバを提供していないことを返します.リクエストの転送でもあります.
Nginxは逆エージェントの2つのドメイン名が同じnginxサーバを指し、ユーザーが異なるドメイン名にアクセスして異なるWebページの内容を表示することを実現します.2つのドメイン名はwww.sian.com.cnとwww.sohu.comnginxサーバは仮想マシン192.168を使用する.101.3
ステップ1:2つのtomcatをインストールし、それぞれ8080ポートと8081ポートで実行します.ステップ2:tomcatを2つ起動します.ステップ3:リバースエージェントサーバの構成upstream tomcat 1{server 192.168.25.148:8080;}server {listen 80;server_name www.sina.com.cn;
ステップ4:nginxプロファイルの再ロードステップ5:ドメイン名を構成hostsファイルにドメイン名とipのマッピング関係を追加する192.168.25.148 www.sina.com.cn192.168.25.148 www.sohu.com
負荷等化1つのサービスが複数のサーバから提供される場合、負荷を異なるサーバに割り当てる必要があり、負荷等化が必要である.upstream tomcat2 {server 192.168.25.148:8081;server 192.168.25.148:8082;}サーバの実際の状況に応じてサーバウェイトを調整できます.ウェイトが高いほど割り当てられたリクエストが多くなり、ウェイトが低いほどリクエストが少なくなります.デフォルトはすべて1 upstream tomcat 2{server 192.168.25.148:8081;server 192.168.25.148:8082 weight=2;}
Nginxの高可用性nginxの高可用性を実現するには、バックアップマシンを実現する必要があります.
keepalived+nginxプライマリ・プロビジョニング
nginx
steamモジュール転送tcpはsocketに負荷等化を行う
次のように構成されています.
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;
}
stream{
log_format proxy '$remote_addr [$time_local] '
'$protocol $status $bytes_sent $bytes_received '
'$session_time "$upstream_addr" '
'"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';
open_log_file_cache off;
access_log logs/tcp-access.log proxy ;
upstream test{
server xxx.xxx.xxx.xxx:443 weight=1 max_fails=1 fail_timeout=30s;
server xxx.xxx.xxx.xxx:443 weight=1 max_fails=1 fail_timeout=30s;
}
server{
listen 4438;
proxy_pass test;
proxy_connect_timeout 8s;
proxy_timeout 7d;
}
}
upstreamモジュールはweb socketを転送して負荷均衡を行う
次のように構成されています.
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
access_log logs/access.log main;
sendfile on;
keepalive_timeout 65;
upstream wsbackend {
server 127.0.0.1:9123;
}
upstream service {
server 127.0.0.1:8080 weight=1 max_fails=3 fail_timeout=30s;
}
server {
listen 443 ssl;
server_name localhost;
ssl_certificate ../ssl/icetech.com.cn.pem;
ssl_certificate_key ../ssl/icetech.com.cn.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location /service {
proxy_pass http://service;#
proxy_set_header X-Real-IP $remote_addr;
proxy_next_upstream http_502 http_504 error timeout invalid_header;
}
location / {
proxy_pass http://wsbackend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
}
共通の構成
#user nobody; #
worker_processes 1; # , , CPU
#error_log logs/error.log; #
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid; #pid
events {
#use epoll;
# epoll I/O
# :
# apache ,nginx ,
#A)
#Select、poll , ,nginx select poll
#B)
#Kqueue: FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 MacOS X. MacOS #X kqueue 。
#Epoll: Linux 2.6 。
#/dev/poll: Solaris 7 11/99+, HP/UX 11.22+ (eventport), IRIX 6.5.15+ Tru64 UNIX 5.1A+。
#Eventport: Solaris 10. ,
worker_connections 1024;
# , , , , cpu 100%
# , nginx worker_processes*worker_connections
keepalive_timeout 60;
}
# http ,
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
# Tomcat
upstream B2C_WEB {
#ip_hash; # ip , Tomcat
server 192.168.126.1:8080 weight=1 max_fails=3 fail_timeout=30s;
server 192.168.126.15:8080 weight=1 max_fails=3 fail_timeout=30s;
#weight=1
# , 192.168.126.1 | 15 30 3 ,
# , 30 nginx 。
}
server {
listen 80; #
server_name localhost;
charset UTF-8;
index index.html index.htm index.jsp index.do;
root E:\\JavaEE\\Apache-Tomcat-7.0.41\\wtpwebapps; #WEB
location ~ ^/(WEB-INF)/ {
deny all; # WEB-INF
}
# Tomcat
location ~ .*\.(jsp|jspx|do)?$ {
proxy_pass http://B2C;#
proxy_set_header X-Real-IP $remote_addr;
proxy_next_upstream http_502 http_504 error timeout invalid_header;
}
# Tomcat
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d; #
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
# Nginx
location /Nginxstatus {
stub_status on;
access_log off;
}
#charset koi8-r;
#access_log logs/host.access.log main;
#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;
}
}
}
nginxNginxとは高性能のhttpサーバ/リバースエージェントサーバおよびEメール(IMAP/POP 3)エージェントサーバである.ロシアのプログラマーIgor Sysoevが開発したもので、公式テストではnginxが5万同時リンクをサポートし、cpu、メモリなどのリソース消費が非常に低く、動作が非常に安定している.
シーン1、httpサーバを適用します.Nginxはhttpサービスであり、httpサービスを独立して提供することができる.Web静的サーバを作成できます.2、仮想ホスト.1台のサーバで複数のWebサイトを仮想化できます.たとえば、パーソナルWebサイトで使用される仮想ホスト.3、リバースエージェント、負荷バランス.Webサイトへのアクセスがある程度になると、単一のサーバがユーザの要求を満たすことができない場合、nginxを逆エージェントとして複数のサーバクラスタを使用する必要があります.また、複数のサーバは、負荷を平均的に分担することができ、あるサーバの負荷が高いため、あるサーバがアイドル状態になることはありません.
nginxインストールダウンロードnginx:
:
http://nginx.org/
要求される実装環境1、gccを実装する必要がある環境.yum install gcc-c++2、サードパーティの開発パッケージ.PCREPCRE(Perl Compatible Regular Expressions)は、perl互換性のある正規表現ライブラリを含むPerlライブラリです.nginxのhttpモジュールはpcreを使用して正規表現を解析するので、linuxにpcreライブラリをインストールする必要があります.yum install-y pcre pcre-devel注:pcre-develはpcreを使用して開発された二次開発ライブラリです.nginxにもこのライブラリが必要です.zlibzlibライブラリは圧縮と解凍の方法を多く提供しており、nginxはzlibを使用してhttpパッケージの内容をgzipするため、linuxにzlibライブラリをインストールする必要があります.yum install -y zlib zlib-devel
openssl
OpenSSL , 、 SSL , 。
nginx http , https( ssl http), linux openssl 。
yum install -y openssl openssl-devel
インストール手順
: nginx linux
:
[root@localhost ~]# tar zxf nginx-1.8.0.tar.gz
: configure makeFile 。
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi
: nginx , /var/temp/nginx, /var temp nginx
[root@localhost sbin]# mkdir /var/temp/nginx/client -p
:make
:make install
nginxを起動してsbinディレクトリに入る[root@localhost sbin]# ./nginxを閉じるnginx:[root@localhost sbin]# ./nginx-s stop推奨使用:[root@localhost sbin]# ./nginx -s quit
nginx:
1、 。
2、 :
[root@localhost sbin]# ./nginx -s reload
仮想ホストの構成は、1台のサーバで複数のWebサイトを起動することです.異なるウェブサイトを区別する方法:1、ドメイン名が異なる2、ポートが異なる
ポートによって異なる仮想マシンNginxのプロファイルを区別する:/usr/local/nginx/conf/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 server, 。
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#Html nginx html
root html;
index index.html index.htm;
}
}
}
:
#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;
}
}
server {
listen 81;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html-81;
index index.html index.htm;
}
}
}
ドメイン名で仮想ホストとドメイン名を区別するのがウェブサイトです.www.baidu.comwww.taobao.comwww.jd.comTcp/ip
Dns : ip 。 ip 。
:
Baidu.com
Taobao.com
Jd.com
:
www.baidu.com
Image.baidu.com
Item.baidu.com
:
1.Image.baidu.com
Aaa.image.baidu.com
ip , ip 。
hosts 。
window hosts :(C:\Windows\System32\drivers\etc)
ip , hosts ip , dns 。
Nginx
#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;
}
}
server {
listen 81;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html-81;
index index.html index.htm;
}
}
server {
listen 80;
server_name www.taobao.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html-taobao;
index index.html index.htm;
}
}
server {
listen 80;
server_name www.baidu.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html-baidu;
index index.html index.htm;
}
}
}
:
192.168.25.148 www.taobao.com
192.168.25.148 www.baidu.com
リバースエージェントリバースエージェントサーバは、どのサーバがサービスを提供しているかを決定し、プロキシサーバがサーバを提供していないことを返します.リクエストの転送でもあります.
Nginxは逆エージェントの2つのドメイン名が同じnginxサーバを指し、ユーザーが異なるドメイン名にアクセスして異なるWebページの内容を表示することを実現します.2つのドメイン名はwww.sian.com.cnとwww.sohu.comnginxサーバは仮想マシン192.168を使用する.101.3
ステップ1:2つのtomcatをインストールし、それぞれ8080ポートと8081ポートで実行します.ステップ2:tomcatを2つ起動します.ステップ3:リバースエージェントサーバの構成upstream tomcat 1{server 192.168.25.148:8080;}server {listen 80;server_name www.sina.com.cn;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://tomcat1;
index index.html index.htm;
}
}
upstream tomcat2 {
server 192.168.25.148:8081;
}
server {
listen 80;
server_name www.sohu.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://tomcat2;
index index.html index.htm;
}
}
ステップ4:nginxプロファイルの再ロードステップ5:ドメイン名を構成hostsファイルにドメイン名とipのマッピング関係を追加する192.168.25.148 www.sina.com.cn192.168.25.148 www.sohu.com
負荷等化1つのサービスが複数のサーバから提供される場合、負荷を異なるサーバに割り当てる必要があり、負荷等化が必要である.upstream tomcat2 {server 192.168.25.148:8081;server 192.168.25.148:8082;}サーバの実際の状況に応じてサーバウェイトを調整できます.ウェイトが高いほど割り当てられたリクエストが多くなり、ウェイトが低いほどリクエストが少なくなります.デフォルトはすべて1 upstream tomcat 2{server 192.168.25.148:8081;server 192.168.25.148:8082 weight=2;}
Nginxの高可用性nginxの高可用性を実現するには、バックアップマシンを実現する必要があります.
nginx , nginx, nginx , nginx web , 。
, 。 (High Availability) , “I am alive” 。 , IP ; “I am alive” , IP , 。
keepalived+nginxプライマリ・プロビジョニング
keepalived
keepalived , 。
Keepalived web , web , ,Keepalived , web , web Keepalived web , , , web 。
keepalived
keepalived VRRP ,VRRP Virtual Router Redundancy Protocol, 。
, , N , master backup,master vip(VIP = Virtual IP Address, IP , vip),master , backup VRRP master , VRRP backup master。 。
keepalived , core、check VRRP。core keepalived , 、 。check , 。VRRP VRRP 。
keepalived+nginx
:
user
|(0)
|
vip 192.168.101.100
/
/
/(1)
/
/
/
nginx nginx
( ) ( )
192.168.101.3 192.168.101.3
keepalived keepalived
\
\
\ (2)
\
\
tomcat
0 ip
1 nginx
2 nginx tomcat
:
user
|(2)
|
vip 192.168.101.100
\
\
(1) \(3)
\
\
\
nginx nginx
( ) ( )
192.168.101.3 (0) 192.168.101.3
keepalived keepalived
/
/
/
/(4)
/
tomcat
0
1
2 ip
3 nginx
4 nginx tomcat
:
user
|(0)
|
vip 192.168.101.100
/
/
/(1)
/
/
/
nginx nginx
( ) ( )
192.168.101.3 192.168.101.3
keepalived keepalived
\
\
\ (2)
\
\
tomcat
0
1
2 ip
3 nginx
4 nginx tomcat
nginx, :192.168.101.3 192.168.101.4
tomcat :192.168.101.5、192.168.101.6
keepalived
nginx keepalived, keepalived.txt :