Nginx Web快速入門—基礎
Nginx的概述
Nginx是什麼
## Nginx是一個開源且高性能、可靠的Http Web服務、代理服務
開源:直接獲取源代碼,GPL協議(GPL協議:開源組織規定:只要軟件打上GPL這個標籤,軟件一概開源不收費,能夠隨意修改源代碼,二次開發後必須開源出來,不收費)
高性能:支持海量併發(併發是同一時刻的訪問量)
可靠:服務穩定
靜態Web軟件
nginx:官方
apache
IIS
lighttpd
tengine:第三方開發(阿里,淘寶開發)
openresty-nginx:第三方開發,支持LUA語言
動態的Web軟件
Tomcat:javar的容器
Resin:javar的容器
weblogic:收費,穩定
Jboss
選擇Nginx的緣由
Nginx很是輕量
功能模塊少 (源代碼僅保留http與核心模塊代碼,其他不夠核心代碼會做爲插件來安裝)
代碼模塊化 (易讀,便於二次開發,對於開發人員很是友好)
互聯網公司都選擇Nginx
1.Nginx技術成熟,具有的功能是企業最常使用並且最須要的
2.適合當前主流架構趨勢, 微服務、雲架構、中間鍵
3.統一技術棧, 下降維護成本, 下降技術更新成本(防火牆,負載均衡,web,均可以是nginx來作)
Nginx採用Epool網絡模型,Apache採用Select模型
Select: 當用戶發起一次請求,select模型就會進行一次遍歷掃描,從而致使性能低下。
Epool: 當用戶發起請求,epool模型會直接進行處理,效率高效,並沒有鏈接限制。
Nginx應用場景
靜態服務 |
代理服務 |
安全服務 |
流行架構 |
瀏覽器緩存 |
協議類型 |
訪問控制 |
Nginx+PHP(Fastcgi_pass)LNMP |
防資源盜用 |
正向代理 |
訪問限制 |
Nginx+Java(Proxy_Pass)LNMT |
資源分類 |
反向代理 |
流量限制 |
Nginx+Vue+Python(uwsgi_Pass) |
資源壓縮 |
負載均衡 |
攔截攻擊 |
|
資源緩存 |
代理緩存 |
攔截異常請求 |
|
跨越訪問 |
動靜分離 |
攔截SQL注入 |
|
源碼安裝Nginx
Nginx的官方網站:TP
[root@web02 ~]# mkdir /source_code
[root@web02 ~]# cd /source_code/
[root@web01 source_code]# wget https://nginx.org/download/nginx-1.20.1.tar.gz
[root@web02 /source_code]# ll
-rw-r--r-- 1 root root 1061461 May 25 23:34 nginx-1.20.1.tar.gz
[root@web02 /source_code]# yum install -y zlib-devel
[root@web02 /source_code]# yum install -y pcre-devel
[root@web02 /source_code]# tar xf nginx-1.20.1.tar.gz
[root@web02 /source_code]# ll
drwxr-xr-x 8 1001 1001 158 May 25 20:35 nginx-1.20.1
-rw-r--r-- 1 root root 1061461 May 25 23:34 nginx-1.20.1.tar.gz
[root@web02 /source_code/nginx-1.20.1]# mkdir /app
[root@web02 /source_code/nginx-1.20.1]# ./configure --prefix=/app/nginx-1.20.1
[root@web02 /source_code/nginx-1.20.1]# ll
drwxr-xr-x 6 1001 1001 326 Jul 16 00:54 auto
-rw-r--r-- 1 1001 1001 311503 May 25 20:35 CHANGES
-rw-r--r-- 1 1001 1001 475396 May 25 20:35 CHANGES.ru
drwxr-xr-x 2 1001 1001 168 Jul 16 00:54 conf ##nginx配置文件
-rwxr-xr-x 1 1001 1001 2590 May 25 20:35 configure ##生成
drwxr-xr-x 4 1001 1001 72 Jul 16 00:54 contrib
drwxr-xr-x 2 1001 1001 40 Jul 16 00:54 html
-rw-r--r-- 1 1001 1001 1397 May 25 20:35 LICENSE
-rw-r--r-- 1 root root 442 Jul 16 02:56 Makefile
drwxr-xr-x 2 1001 1001 21 Jul 16 00:54 man
drwxr-xr-x 3 root root 125 Jul 16 02:56 objs
-rw-r--r-- 1 1001 1001 49 May 25 20:35 README
drwxr-xr-x 9 1001 1001 91 Jul 16 00:54 src ##源碼存放目錄
[root@web02 /source_code/nginx-1.20.1]# make
[root@web02 /source_code/nginx-1.20.1]# make install
[root@web02 /app/nginx-1.20.1]# ll
drwxr-xr-x 2 root root 333 Jul 16 03:05 conf ## 配置文件
drwxr-xr-x 2 root root 40 Jul 16 03:05 html ## 站點目錄
drwxr-xr-x 2 root root 6 Jul 16 03:05 logs ##日誌文件
drwxr-xr-x 2 root root 19 Jul 16 03:05 sbin ##啓動的命令
[root@web02 /app/nginx-1.20.1]# ln -s /app/nginx-1.20.1/ /app/nginx
[root@web02 /app]# ll
lrwxrwxrwx 1 root root 18 Jul 16 03:14 nginx -> /app/nginx-1.20.1/
drwxr-xr-x 6 root root 54 Jul 16 03:05 nginx-1.20.1
[root@web02 /app]# vim /etc/profile
PATH="/app/nginx/sbin:$PATH"
[root@web02 /app]# echo $PATH
/app/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@web02 /app]# source /etc/profile
[root@web02 /app]# nginx -t
nginx: the configuration file /app/nginx-1.20.1/conf/nginx.conf syntax is ok
nginx: configuration file /app/nginx-1.20.1/conf/nginx.conf test is successful
[root@web02 /app]# nginx
## 中止nginx
[root@web02 /app]# nginx -s stop
[root@web02 /app]# ps -ef|grep [n]ginx
root 11728 7926 0 03:41 pts/1 00:00:00 grep --color=auto nginx
## 從新加載
[root@web02 /app]# nginx -s reload
[root@web02 /app]# ps -ef|grep [n]ginx
root 11689 1 0 03:32 ? 00:00:00 nginx: master process nginx
nobody 11690 11689 0 03:32 ? 00:00:00 nginx: worker process
[root@web02 /app]# netstat -lntup|grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 11689/nginx: master
打開瀏覽器:http://10.0.0.8html
![image](http://static.javashuo.com/static/loading.gif)
Yum安裝nginx
官網找安裝nginx源的配置信息
![image](http://static.javashuo.com/static/loading.gif)
![image](http://static.javashuo.com/static/loading.gif)
![image](http://static.javashuo.com/static/loading.gif)
[root@web02 /app]# 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
[root@web02 /etc/yum.repos.d]# yum install -y nginx
[root@web02 /etc/yum.repos.d]# /sbin/nginx -v
nginx version: nginx/1.20.1
[root@web02 /etc/yum.repos.d]# /sbin/nginx -V
--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'
Nginx相關配置文件
1.Nginx主配置文件
路徑 |
類型 |
做用 |
/etc/nginx/nginx.conf |
配置文件 |
nginx主配置文件 |
/etc/nginx/conf.d/default.conf |
配置文件 |
默認網站配置文件(虛擬主機配置文件) |
2.Nginx代理相關參數文件
路徑 |
類型 |
做用 |
/etc/nginx/fastcgi_params |
配置文件 |
Fastcgi代理配置文件 |
/etc/nginx/scgi_params |
配置文件 |
scgi代理配置文件 |
/etc/nginx/uwsgi_params |
配置文件 |
uwsgi代理配置文件 |
3.Nginx編碼相關配置文件
路徑 |
類型 |
做用 |
/etc/nginx/win-utf |
配置文件 |
Nginx編碼轉換映射文件 |
/etc/nginx/koi-utf |
配置文件 |
Nginx編碼轉換映射文件 |
/etc/nginx/koi-win |
配置文件 |
Nginx編碼轉換映射文件 |
/etc/nginx/mime.types |
配置文件 |
Nginx編碼轉換映射文件 |
4.Nginx管理相關命令
路徑 |
類型 |
做用 |
/usr/sbin/nginx |
命令 |
Nginx命令行管理終端工具 |
/usr/sbin/nginx-debu |
命令 |
Nginx命令行與終端調試工具 |
5.Nginx日誌相關目錄與文件
路徑 |
類型 |
做用 |
/var/log/nginx |
目錄 |
Nginx默認存放日誌目錄 |
/etc/logrotate.d/nginx |
配置文件 |
Nginx默認的日誌切割 |
Nginx的配置文件講解
[root@web02 ~]# vim /etc/nginx/nginx.conf
-------------------------- 核心模塊 -------------------------
## 服務的啓動用戶
user nginx;
## worker進程(子進程),根據cpu的核心數
worker_processes auto;
## 錯誤日誌,以及日誌的級別
error_log /var/log/nginx/error.log notice;
## pid文件的存放路徑
pid /var/run/nginx.pid;
-------------------------------------------------------------
----------------------- 事件驅動模塊 -------------------------
events {
## 每一個worker進程最大鏈接數
worker_connections 1024;
}
-------------------------------------------------------------
------------------------ HTTP網站配置 ----------------------------
http {
server {
}
## 默認nginx支持的文件類型
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;
#tcp_nopush on;
## 長連接超時時間
keepalive_timeout 65;
## 傳輸過程當中壓縮
#gzip on;
## 虛擬主機相關配置(網站的配置)
server {
## 監聽在80端口
listen 80;
## IPv6
listen [::]:80;
## 配置訪問的域名或者IP
server_name _;
# 該網站的站點目錄
root /usr/share/nginx/html;
## 404頁面的報錯路徑
error_page 404 /404.html;
## 404URL
location = /404.html {
}
## 5xx頁面的報錯路徑
error_page 500 502 503 504 /50x.html;
## 5xxURL
location = /50x.html {
}
}
## 包含全部nginx的虛擬主機配置文件
include /etc/nginx/conf.d/*.conf;
}
-------------------------------------------------------------