網站首頁 編程語言 正文
Nginx命令行
默認啟動方式,直接使用二進制程序,讀取配置文件conf/nginx.conf
/usr/local/nginx/sbin/nginx
指定配置文件的啟動方式,使用-c參數后指定的nginx.conf配置文件來啟動nginx
/usr/local/nginx/sbin/nginx -c /tmp/nginx.conf
另行指定安裝目錄的啟動方式
/usr/local/nginx/sbin/nginx -p /tmpr/nginx/
可以通過-g參數臨時指定一些全局配置項,以使新的配置生效
/usr/local/nginx/sbin/nginx -g “pid /var/nginx/test.pid;”
## g參數的約束條件
一是指定的配置項不能與默認路徑下的nginx.conf中國的配置項相沖突,否則無法啟動;
二是以-g方式啟動的Nginx服務執行其他命令行時,需要把-g參數也帶上,如停止nginx服務
/usr/local/nginx/sbin/nginx -g “pid /var/nginx/test.pid;” -s stop
測試配置信息是否有錯誤
# 在不啟動nginx情況下,使用-t參數僅測試配置文件是否有錯誤
/usr/local/nginx/sbin/nginx -t
# 測試階段不輸出信息
/usr/local/nginx/sbin/nginx -t -q
顯示版本信息
/usr/local/nginx/sbin/nginx -v
顯示編譯階段的參數
/usr/local/nginx/sbin/nginx -V
快速停止服務
/usr/local/nginx/sbin/nginx -s stop
kill -s SIGTERM nginx_pid(nginx master pid)
優雅停止服務
/usr/local/nginx/sbin/nginx -s quit
只停止某個worker進程
kill -s SIGTERM <nginx worker pid>
使運行中的nginx重讀配置并生效
/usr/local/nginx/sbin/nginx -s reload
kill -s SIGHUP <nginx master pid>
日志文件回滾
/usr/local/nginx/sbin/nginx -s reopen
kill -s SIGUSR1 <nginx master pid>
平滑升級nginx
kill -s SIGUSR2 <nginx master pid>
此時會將nginx的pid文件重命名,如將/usr/local/nginx/logs/nginx.pid rename為
/usr/local/nginx/logs/nginx.pid.oldbin
使用上述啟動命令開啟新的Nginx服務,這時通過ps命令可發現新舊版本的Nginx在同時運行
通過kill命令向舊版本的master進程發送SIGQUIT信號,以”優雅”的方式關閉舊版本的Nginx
顯示命令行幫助
/usr/local/nginx/sbin/nginx -h
Nginx常用配置
nginx基本配置
http{
gzip on;
# 配置負載均衡
upstream service{
server 192.168.10.221:9091 weight 2;
server 192.168.10.221:9092 weight 3;
server 192.168.10.221:9093 weight 2;
}
# ip hash負載
upstream backend {
ip_hash;
server backend1. example.com;
server backend2. example.com;
server backend3. example.com down; #down表示上游服務器永久下線,只在使用ip_hash配置項時才有用
server backend4. example.com;
}
# 配置多個虛擬主機server
server{
listen [ip]:80;
location /webstatic{
}
location ~*.(jpg|jpeg|png|jpe|gif)${
}
}
}
靜態資源配置
location ~* /static/*.+\.(gif|jpg|png|css|js|flv|ico|swf)$ {
alias /home/ubuntu/mywork/python/youhuiquan/website/static;
expires 1d;
# proxy_pass http://static;
# proxy_redirect off;
# proxy_cache_valid 200 302 1h;
# proxy_cache_valid 301 1h;
# proxy_cache_valid any 1h;
add_header Pragma public;
add_header Cache-Control "public";
}
移動端配置
location / {
if ( $http_user_agent ~ "(MIDP)|(WAP)|(UP.Browser)|(Smartphone)|(Obigo)|(Mobile)|(AU.Browser)|(wxd.Mms)|(WxdB.Browser)|(CLDC)|(UP.Link)|(KM.Browser)|(UCWEB)|(SEMC\-Browser)|(Mini)|(Symbian)|(Palm)|(Nokia)|(Panasonic)|(MOT\-)|(SonyEricsson)|(NEC\-)|(Alcatel)|(Ericsson)|(BENQ)|(BenQ)|(Amoisonic)|(Amoi\-)|(Capitel)|(PHILIPS)|(SAMSUNG)|(Lenovo)|(Mitsu)|(Motorola)|(SHARP)|(WAPPER)|(LG\-)|(LG/)|(EG900)|(CECT)|(Compal)|(kejian)|(Bird)|(BIRD)|(G900/V1.0)|(Arima)|(CTL)|(TDG)|(Daxian)|(DAXIAN)|(DBTEL)|(Eastcom)|(EASTCOM)|(PANTECH)|(Dopod)|(Haier)|(HAIER)|(KONKA)|(KEJIAN)|(LENOVO)|(Soutec)|(SOUTEC)|(SAGEM)|(SEC\-)|(SED\-)|(EMOL\-)|(INNO55)|(ZTE)|(iPhone)|(Android)|(Windows CE)|(Wget)|(Java)|(curl)|(Opera)" ){
rewrite ^.+ http://m.domainName.com/$uri;
}
rewrite ^.+ http://www.domainName.com/$uri;
}
服務負載均衡基本配置
location / {
proxy_pass http://service;
#proxy_method GET | POST
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr; # 獲取真實IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 獲取代理者的真實IP
proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
proxy_set_header Via "nginx";
# proxy_redirect http://service http://service_others; # service返回302或301的時候將會重定向到service_others路由
# proxy_next_upstream[ error | timeout | invalid_header | http_500 | http_502 | http_503 | http_504 | http_404 | off]; # 當上游服務器出錯的時候將繼續轉發到另一臺上游服務器處理相同的請求
client_max_body_size 60m;
}
uWSGI 配置
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /path/to/your/mysite/uwsgi_params; # the uwsgi_params file you installed
}
php fastcgi配置
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-cgi alone:
#fastcgi_pass 127.0.0.1:9001;
# With php5-fpm:
#fastcgi_pass unix:/var/run/php5-fpm.sock;
#fastcgi_index index.php;
#root /home/crazysal/public_html;
fastcgi_pass 127.0.0.1:9001;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
nginx解決圖片資源沒有后綴名訪問時直接下載問題
location / {
root /path/static;
add_header content-type "image/jpeg";
expires 1d;
// ...其他相關的配置
}
nginx 配置錯誤碼跳轉頁面
# 當服務端出現以下的狀態碼的時候將會跳轉到50x的頁面,此時還需要配置50x的頁面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
internal; # 客戶端無法直接訪問,必須是由nginx服務器內部轉發的
root errors/html;
}
# 為location配置一個名稱 (也是只能內部轉發)
error_page 500 502 503 504 = @errors;
location @errors {
proxy_pass http://error_backend;
}
# location 查詢順序和優先級
1. 帶有 = 精確匹配優先
2. 沒有修飾符的精確匹配優先
3. 正則表達式按照它們在配置文件中定義的順序
4. 帶有 ^~ 修飾的開頭匹配
5. 帶有 ~ 或者 ~* 修飾的,如果正則與URI匹配
6. 沒有修飾的,指定字符串與URI開頭匹配
try_files
# 按照指定的順序檢查存在的文件,并且返回第一個找到的文件結果,如果所有文件都沒有找到,那么將啟用最后一個參數命名的內部重定向
upstream render_uri{
server 127.0.0.1:9080 weight=9;
}
try_files path1 path2 @render
location @render{
proxy_pass http://render_uri;
}
加證書SSL配置支持https
server {
listen 443;
listen [::]:443;
charset UTF-8;
server_name domain;
gzip on;
gzip_types application/json; # text/html,application/json格式總是會被壓縮
ssl on;
ssl_certificate /etc/nginx/ssl-certs/xx.crt;
ssl_certificate_key /etc/nginx/ssl-certs/xx.key;
error_log /home/ubuntu/java/logs/nginx/nginx_error.log;
access_log /home/ubuntu/java/logs/nginx/nginx_access.log main_log;
root /home/ubuntu/test;
index index.html;
location /album {
proxy_pass http://album;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
proxy_set_header Via "nginx";
client_max_body_size 60m;
}
location /dounile/images {
root /home/ubuntu/java/dounile/images;
expires 1d;
}
location /dounile {
proxy_next_upstream http_502 http_504 timeout; # 配置故障轉移,如果服務出現502,504或者是timeout則將轉發給下一個
valid_referers blocked dtreess.com *.dtreess.com; # 配置簡單的防盜鏈,只有dtreess.com的域名下才能訪問
proxy_pass http://dounile;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
proxy_set_header Via "nginx";
client_max_body_size 60m;
}
}
全局負載均衡
http{
geo $geo{
default default;
203.101.10.10/24 A; # 客戶端滿足IP段走A服務器
109.23.23.10/28 B; # 客戶端滿足IP段走B服務器
}
upstream default.server {
server 192.168.10.1 weight=10;
}
upstream A.server{
server 192.168.10.2 weight=10;
}
upstream B.server{
server 192.168.10.3 weight=10;
}
server {
listen 80;
location / {
proxy_pass: http://$geo.server/$request_uri;
}
}
}
rewrite 模塊
# 執行url的重定向,有利于去掉惡意訪問的url,也有利于搜索引擎優化
last: 完成重寫指令,之后搜索相應的URI或location
break:完成重寫指令
redirect:返回302的臨時重定向,如果替換字段用http://開頭則被使用
permanent:返回301永久重定向
nginx查看進程進行追蹤
# 打開shell終端
strace -f pid
# 打開shell第二個終端
nginx -s reload
# 在上面的strace將跟蹤進程啟動過程并輸出到控制臺中
設置nginx常用的日志格式
log_format log_main_format '$ remote_addr - $ remote_user [$ time_local] $ request ' 'upstream_response_time $ upstream_response_time ' 'msec $ msec request_time $ request_time'; log_format up_head '$ remote_addr - $ remote_user [$ time_local] $ request ' 'upstream_http_content_type $ upstream_http_content_type';
'$remote_addr - $remote_user [$time_local] $request' 'req_time $request_time' 'upstream_response_time $upstream_response_time' 'http_host $http_host' 'status $status' 'http_x_forwarded_for $http_x_forwarded_for' 'referer $http_referer' 'upstream_addr $upstream_addr' 'bytes $body_bytes_sent' 'request_body $request_body' 'upstream_status $upstream_status' 'uri $upstream_scheme://$upstream_host$upstream_uri/' 'agent $http_user_agent'
nginx優化配置
1. 盡量提高單臺機器的處理效率
2. 盡量降低單臺機器的負載
3. 降低磁盤IO
4. 降低網絡IO
5. 減少內存使用
6. 高效利用CPU
# nginx.config
user keithl;
work_processes 8;
http{
events{
use epoll;
# worker_connections的設置與物理內存有關,因為系統可以打開的最大文件數與內存成正比,一般1G內存機器可以打開的文件數是10萬個
worker_connections 1024; # 并發總數 = work_processes * worker_connections,需要根據設置進程數和系統可以打開的文件數進行調整
}
}
原文鏈接:https://blog.csdn.net/wind_602/article/details/82764496
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-10-08 Qt動態庫調用宿主進程中的對象方法純虛函數使用_C 語言
- 2022-07-06 pandas實現一行拆分成多行_python
- 2022-05-22 jQuery中的關系查找方法_jquery
- 2022-09-10 C#實現自定義線程池實例代碼_C#教程
- 2022-09-15 .Net站點設置多個路由對應同一個Action_實用技巧
- 2024-01-10 給idea添加右鍵打開功能
- 2024-03-05 layui彈出層的表單驗證(form表單自帶的驗證不執行)
- 2022-09-16 Python中的socket網絡模塊介紹_python
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支