nginx 是一個開源且高性能, 可靠的Http web服務, 代理服務, 負載均衡html
開源: 直接獲取源代碼linux
高性能: 高併發nginx
可靠: 服務穩定web
輕量: 佔用資源少apache
常見的HTTP Web服務vim
httpd 由apache基金會centos
IIS 微軟服務器版跨域
GWS google開發瀏覽器
Openrestry 基於nginx + lua緩存
Tengline 淘寶基於nginx開發
市場佔有率: apache>IIS>nginx
nginx 在互聯網行業使用比較普遍
Nginx很是輕量
功能模塊少(源代碼僅保留http與核心模塊代碼, 其他不夠核心代碼會做爲插件來安裝)
代碼模塊化 (易讀, 便於二次開發, 對於開發人員很是友好)
server { location /nba { } location /cba { } } server { location /lxx { } location /lyy { } }
nginx 適合作微服務, 雲架構, 中間層
nginx 採用Epool網絡模型, apache 採用select模型
https://blog.csdn.net/foxgod/article/details/93165645
代理的優勢就是能夠緩存,流程爲PC把請求發送給代理服務器, 代理服務器訪問到了服務器, 緩存下來, 再提供給PC, 當PC下次再訪問, 則更快
代理服務器能夠限制訪問頁面大小(緩存大小), 屏蔽敏感詞彙
http://www.javashuo.com/article/p-hzbmgtqa-c.html
正向代理(客戶端代理)
"代理服務器"代理了"客戶端",去和"目標服務器"進行交互, 隱藏客戶端的IP, web服務器上只能看到代理服務器的IP
反向代理(服務器代理)
外訪內,是指以代理服務器來接受internet上的鏈接請求,而後將請求轉發給內部網絡上的服務器,並將從服務器上獲得的結果返回給internet上請求鏈接的客戶端,此時代理服務器對外就表現爲一個反向代理服務器
使用場景就是負載均衡, 當受到攻擊時候, 暴露的IP是負載均衡的IP, web服務器不會受影響
訪問控制
基於身份認證的
http://nginx.org/en/linux_packages.html#RHEL-CentOS
$ yum install yum-utils $ vim /etc/yum.repos.d/nginx.repo [nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true $ yum install nginx -y
$ nginx -v nginx version: nginx/1.16.1
$ rpm -ql nginx
/etc/nginx /etc/nginx/nginx.conf /etc/nginx/conf.d /etc/nginx/conf.d/default.conf
/etc/nginx/uwsgi_params /etc/nginx/scgi_params /etc/nginx/fastcgi_params
/etc/nginx/koi-utf /etc/nginx/koi-win /etc/nginx/win-utf
/etc/nginx/mime.types
/usr/lib/systemd/system/nginx.service -----> systemctl reload nginx
/etc/logrotate.d/nginx
/usr/sbin/nginx -----> nginx -s reload /usr/sbin/nginx-debug /usr/lib64/nginx /usr/lib64/nginx/modules
/usr/share/nginx /usr/share/nginx/html /usr/share/nginx/html/50x.html /usr/share/nginx/html/index.html
/usr/share/doc/nginx-1.16.1 /usr/share/doc/nginx-1.16.1/COPYRIGHT /usr/share/man/man8/nginx.8.gz
/var/cache/nginx
/var/log/nginx
$ nginx -V 2>&1 |grep 'prefix'|cut -d: -f2 --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --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=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
--prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --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=nginx --group=nginx
--with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC'
--with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
若是以前的nignx經過源碼安裝, 這個時候部署新的nginx服務器, 如何部署
經過 nginx -v 得到版本
經過nginx -V 得到configure選項
user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/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 /var/log/nginx/access.log main; sendfile on; keepalive_timeout 65; include /etc/nginx/conf.d/*.conf; }
核心模塊, 全局配置, 能夠有events, http
user nginx; # nginx進程所使用的用戶 worker_processes 1; # nginx運行的work進程數量(建議和CPU數量一直或auto) error_log /var/log/nginx/error.log warn; # nginx錯誤日誌存放路徑 pid /var/run/nginx.pid; # nginx服務運行後產生的pid進程號
事件驅動模塊
events { worker_connections 1024; # 每一個worker進程支持的最大鏈接數 use epool; # 事件驅動模型, epoll默認 }
內核模塊
http模塊曾容許有多個server曾, server主要用於配置多個網站
server層容許多個Location, Location主要用於定義網站訪問路徑
http { include /etc/nginx/mime.types; # html渲染所支持的樣式 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 /var/log/nginx/access.log main; sendfile on; # 是否能發送文件 #tcp_nopush on; keepalive_timeout 65; # 長鏈接超時時間65s #gzip on; # 是否壓縮 include /etc/nginx/conf.d/*.conf; # 模塊導入路徑 # 使用server模塊來配置網站, 每一個server{} 表明一個網站(簡稱虛擬主機) server { listen 80; #監聽端口 server_name: localhost; # 提供服務器的域名或主機名 access_log host.access.log; # 訪問日誌 # 控制網站訪問路徑 location / { root /usr/share/nginx/html; # 存放網站代碼路徑 index index.html index.htm; # 服務器返回的默認頁面文件 } # 指定錯誤代碼, 統必定義錯誤頁面, 錯誤代碼重定向到新的Location error_page 500 502 503 504 /50x.html; } }
當咱們訪問game.weixinyu.com的時候, 訪問/weixinyu_code/裏面的頁面代碼, 而且響應頭要有business和author這兩個字段
$ vim /etc/nginx/conf.d/weixinyu.conf server { listen 80; server_name game.weixinyu.com; location / { root /weixinyu_code; index index.html index.htm; add_header business ops; add_header authors weixinyu; } }
$ nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
$ mkdir /weixinyu_code
$ mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.off
echo 'weixinyu' > /weixinyu_code/index.html
本地hosts文件作本地DNS解析 C:\Windows\System32\drivers\etc\hosts
須要修改文件權限, 右擊屬性-安全-Users, 所有容許
192.168.221.20 game.weixinyu.com
$ systemctl restart nginx $ nginx -s reload
$ lsof -i:80 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nginx 9921 root 6u IPv4 41141 0t0 TCP *:http (LISTEN) nginx 9923 nginx 6u IPv4 41141 0t0 TCP *:http (LISTEN)