nginx實踐
[@more@]
Nginx安裝:
]# ./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/bin/ --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
]# make
]# make install
]# /usr/local/bin/nginx -V
nginx: nginx version: nginx/1.0.4
nginx: TLS SNI support disabled
nginx: configure arguments: --prefix=/usr/local/src --sbin-path=/usr/local/bin/ --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
]#
應養成檢查語法的習慣,如下例:
]# /usr/local/bin/nginx -t
nginx: the configuration file /usr/local/src/conf/nginx.conf syntax is ok
nginx: [emerg] getpwnam("www") failed
nginx: configuration file /usr/local/src/conf/nginx.conf test failed
]# useradd www -s /sbin/nologin -M
]# /usr/local/bin/nginx -t
nginx: the configuration file /usr/local/src/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/src/conf/nginx.conf test is successful
啟動nginx:
]# /usr/local/bin/nginx -c /usr/local/nginx/conf/nginx.conf
]# !netstat
netstat -natu | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
nginx主配置檔案:
[root@up_server conf]# pwd
/usr/local/nginx/conf
[root@up_server conf]# ls nginx.conf
nginx.conf
重啟nginx服務命令
]# kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
]# /usr/local/bin/nginx -s reload
error_page 404 /404.html; 為了介面友好
error_page 500 502 503 504 /50x.html; 出現上面錯誤碼,跳轉後面介面
虛擬主機:
(1)基於域名:
server {
listen 80;
server_name
location / {
root sina;
index index.html index.htm;
}
}
server {
listen 80;
server_name
location / {
root baidu;
index index.html index.htm;
}
}
(2)基於IP
server {
listen 192.168.1.25:80;
server_name 192.168.1.25;
location / {
root 254;
index index.html index.htm;
}
}
ab -c 100 n 10000 併發10000次,每次100個請求
正則匹配:
location ~* .*.doc$ {
66 #location ~ .*/.*.(txt|doc) {
67 return 403;
68 #root 254;
69 #deny all;
70 }
location ~* .*.doc$ {
66 #location ~ .*/.*.(txt|doc) {
67 root 254;
68 deny all;
69 }
location /nginx_status {
66 stub_status on;
67 allow 192.168.1.2;
68 deny all;
69 }
此時用1.254來訪問會返回403錯誤資訊
對於圖片的訪問不計入日誌,沒有必要,圖片太多,避免日誌過大
location ~ .*.(jpg|png|css|js|gif)$ {
access_log off;
}
在nginx中作日誌擷取
1.寫一個指令碼 利用mv 笨方法
2.計劃任務 分時日月周
[root@ disk]# cat cut_nginx.sh
#!/bin/bash
# This script run at 00:00
# The Nginx logs path
logs_path="/usr/local/webserver/nginx/logs/"
mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/
mv ${logs_path}access.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y%m%d").log
kill -USR1 `cat /usr/local/webserver/nginx/nginx.pid`
[root@ disk]#
設定訪問許可權:
location / {
root abc; 訪問根目錄
index index.html index.htm;
auth_basic "這是我私有的";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
[root@wang src]# htpasswd -cm /usr/local/src/conf/htpasswd sungy
New password:
Re-type new password:
Adding password for user sungy
[root@wang src]#
再補充一些關於location正則和rewrite相關的學習
nginx反向代理 重點
1。預設是輪詢的 (rr)
keepalive_timeout 65;
upstream server_pool {
server 192.168.1.23:80;
server 192.168.1.25:80;
}
#gzip on;
server {
listen 80;
server_name
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root baidu;
#index index.html index.htm;
proxy_pass
??proxy_set_header Host $host;
}
2。帶有權重(wrr)的叢集
keepalive_timeout 65;
upstream server_pool {
server 192.168.1.23:80 weight=2 max_fails=2 fails_timeout=30; #2次失敗以後,暫停的時間30sOB
server 192.168.1.25:80;
}
#gzip on;
server {
listen 80;
server_name
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root baidu;
#index index.html index.htm;
proxy_pass
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr; 可以看到訪問客戶的真實IP
}
3.hash 解決購物車問題
4.根據伺服器新舊
5.。基於url的hash
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/23168012/viewspace-1052518/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Nginx入門實踐(二)Nginx
- Nginx Ingress 高併發實踐Nginx
- Nginx--進階篇(實踐)Nginx
- Ingress-nginx工作原理和實踐Nginx
- 簡單實踐搭建 nginx 負載均衡Nginx負載
- NGINX限制連線的實踐 (Defense DDOS)Nginx
- Nginx 動態發現方案與實踐Nginx
- 使用Docker快速部署ELK分析Nginx日誌實踐DockerNginx
- 【實戰分享】又拍雲 OpenResty / Nginx 服務優化實踐RESTNginx優化
- Nginx解析漏洞復現以及哥斯拉連線Webshell實踐NginxWebshell
- Spring Boot 2.0(五):Docker Compose + Spring Boot + Nginx + Mysql 實踐Spring BootDockerNginxMySql
- NGINX 入門到企業級應用實踐-基礎篇Nginx
- Vue 前端配置多級目錄實踐(基於Nginx配置方式)Vue前端Nginx
- Nginx部署HTTPS服務過程與異常處理實踐NginxHTTP
- 搞懂分散式技術9:Nginx負載均衡原理與實踐分散式Nginx負載
- K8s叢集nginx-ingress監控告警最佳實踐K8SNginx
- 【原創】一層Nginx反向代理K8S化部署實踐NginxK8S
- 實戰NginxNginx
- Nginx 實踐案例(原始碼編譯安裝方式):利用LNMP搭建wordpress站點Nginx原始碼編譯LNMP
- nginx實用入門Nginx
- 實戰 nginx 調優Nginx
- 從零手寫實現 nginx-03-nginx 基於 Netty 實現NginxNetty
- Golang 高效實踐之併發實踐Golang
- vue實踐06-專案實踐Vue
- Nginx實現請求的負載均衡 + keepalived實現Nginx的高可用Nginx負載
- NGINX LDAP參考實現中的零日漏洞 - nginxNginxLDA
- Nginx-正向代理實現Nginx
- nginx實現平滑升級Nginx
- keepalived + nginx 實現高可用Nginx
- nginx 如何實現 if 巢狀Nginx巢狀
- Nginx 實現 Rewrite 跳轉Nginx
- Devops實現之 nginx(一)devNginx
- nginx實現keepalived高可用Nginx
- Zabbix實戰--監控NginxNginx
- php nginx 實時輸出PHPNginx
- Nginx實現URL重寫Nginx
- nginx實現負載均衡Nginx負載
- 通義靈碼實踐教程——效能實踐
- Golang高效實踐之interface、reflection、json實踐GolangJSON