1.nginx的下載
解壓后文件目錄:
2.nginx的常用命令
nginx -s stop 強制關閉
nginx -s quit 安全關閉
nginx -s reload 改變配置文件的時候,重啟nginx工作進程,來時配置文件生效
nginx -s reopen 打開日志文件
3.nginx的核心:nginx.conf配置文件
#運行nginx的所屬組和所有者
#user nobody;
#表示工作進程的數量,一般設置為cpu的核數
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 {
#文件MIME(Content-Type) 映射表
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 65;
#gzip on;
#設置負載均衡的服務器列表
upstream my_stream {
#weigth參數表示權值,權值越高被分配到的幾率越大
server 127.0.0.1:80 weight=5;
server 127.0.0.1:90 weight=1;
}
#server{}塊定義了虛擬主機,一個server段一般對應一個域名
server {
listen 80;
#監聽域名
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root指定對應uri的資源查找路徑,這里html為相對路徑
root html;
#指定首頁index文件的名稱,可以配置多個,以空格分開。如有多個,按配置順序查找
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;
}
}
#第二個server
server {
listen 90;
server_name localhost;
location / {
root html;
index index_.html index_.htm;
}
#對php后綴的請求進行負載均衡
location ~ .*\.php$ {
...
#請求轉向my_stream中的服務器列表,進行負載均衡
proxy_pass http://my_stream ;
...
}
#設置查看Nginx狀態的地址 訪問:localhost:90/ngnix_status
#active connections – 活躍的連接數量
#server accepts handled requests — 總共處理了11個連接 , 成功創建11次握手, 總共處理了6個請求
#reading — 讀取客戶端的連接數.
#writing — 響應數據到客戶端的數量
#waiting — Nginx 已經處理完正在等候下一次請求指令的駐留連接.
location /ngnix_status {
stub_status on;
access_log off;
}
}
}
4.順帶:localhost,127.0.0.1,本機IP之間的差異
localhost 不聯網 不使用網卡,不受防火牆和網卡限制 本機訪問
127.0.0.1 不聯網 網卡傳輸,受防火牆和網卡限制 本機訪問
本機IP 聯網 網卡傳輸 ,受防火牆和網卡限制 本機或外部訪問