nginx啟動命令和默認配置文件的使用
更新時間:2025年06月16日 14:07:51 作者:Gen鄧艮艮
這篇文章主要介紹了nginx啟動命令和默認配置文件的使用,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
常見命令
# 默認配置文件啟動 ./nginx # 停止 ./nginx -s stop # 重啟,加載默認配置文件 ./nginx -s reload # 啟動指定某個配置文件 ./nginx -c /usr/local/nginx/conf/nginx.conf
nginx.conf配置文件
#user nobody; # 指定Nginx worker進程運行以及用戶組
worker_processes 1;
#error_log logs/error.log; # 錯誤日志的存放路徑
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid; # 進程pid存放路徑
# 事件模塊指令,用來指定Nginx的IO模型,Nginx支持的有select、poll、kqueue、epoll等。不同的是epoll用在Linux平臺上,而kqueue用在BSD系統(tǒng)中,對于Linux系統(tǒng),epoll工作模式是首選
events {
use epoll;
# 定義Nginx每個進程的最大連接數(shù),作為服務(wù)器來說:worker_connections * worker_processes;作為反向代理來說,最大并發(fā)數(shù)量為:worker_connections * worker_processes / 2,因為反向代理服務(wù)器,每個并發(fā)會建立與客戶端的連接和與后端服務(wù)的連接,會占用兩個連接
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
# 自定義服務(wù)日志
#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;
# 是否開啟高效傳輸模式 on開啟 off關(guān)閉
sendfile on;
# 減少網(wǎng)絡(luò)報文段的數(shù)量
#tcp_nopush on;
# 客戶端連接超時時間
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
# 虛擬主機的配置
server {
listen 80; # 虛擬主機的服務(wù)端口
server_name localhost; # 用來指定IP地址或域名,多個域名之間用空格分開
#charset koi8-r;
#access_log logs/host.access.log main;
# URL地址匹配
location / {
root html; # 服務(wù)默認啟動目錄
index index.html index.htm; # 默認訪問文件,按照順序找
}
#error_page 404 /404.html; # 錯誤狀態(tài)碼的顯示頁面
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}location匹配規(guī)則
正則
- ^:開始符號
- $:結(jié)束符號
location路徑匹配
- location = /uri:=表示精準匹配,只有完全匹配上才能生效
- location /uri:不帶任何修飾符,表示前綴匹配
- location ^~ /uri:匹配任何以/uri開頭的匹配到就停止搜索
- location /:通用匹配
正則匹配
- 區(qū)分大小寫匹配:~
- 不區(qū)分大小寫匹配:~*
圖片服務(wù)器
server {
listen 80;
server_name localhost a.com;
location / {
root html;
index index.html index.htm;
}
location /img {
alias /usr/local/img/;
}
}注意
- 在location / 中配置root目錄
- 在location /path 中配置alias虛擬目錄,目錄后面的"/"符號一定要帶上
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Nginx下Wordpress的永久鏈接實現(xiàn)(301,404等)
經(jīng)過多番測試,終于在nginx下實現(xiàn)了rewrite的功能,WrodPress的永久鏈接終于生效了2012-09-09
Nginx+keepalived主從,雙主架構(gòu)詳解
本文詳細介紹了Nginx和Keepalived的配置過程,包括主從架構(gòu)和雙主架構(gòu)的搭建,通過配置Keepalived,實現(xiàn)了VIP的自動切換和高可用性,確保服務(wù)的連續(xù)性和可靠性,同時,還討論了架構(gòu)對比、優(yōu)化方向、常見故障排查和生產(chǎn)環(huán)境最佳實踐2026-01-01
Nginx在高并發(fā)架構(gòu)中配置靜態(tài)文件地址的實戰(zhàn)指南
在高并發(fā)架構(gòu)中,Nginx不僅僅是一個Web服務(wù)器,更是整個系統(tǒng)的流量守門人和性能加速器,為什么你的網(wǎng)站明明用了Nginx,卻依然感覺慢?為什么一上并發(fā)就出現(xiàn)502/504錯誤,今天,我們就深入剖析如何通過精準配置Nginx靜態(tài)文件參數(shù),需要的朋友可以參考下2026-01-01

