nginx基本命令與配置
基本命令
/usr/local/nginx/sbin/nginx -h #幫助
/usr/local/nginx/sbin/nginx -v #顯示版本
/usr/local/nginx/sbin/nginx -V #顯示版本和配置資訊
/usr/local/nginx/sbin/nginx -t #測試配置
-t:測試配置檔案是否正確,在執行時需要重新載入配置的時候,此命令非常重要,用來檢測所修改的配置檔案是否有語法錯誤。
例如我們要測試某個配置檔案是否書寫正確,我們可以使用以下命令: sbin/nginx – t – c conf/nginx2.conf
/usr/local/nginx/sbin/nginx -q #測試配置時,只輸出錯誤資訊
/usr/local/nginx/sbin/nginx -s stop #停止伺服器
/usr/local/nginx/sbin/nginx -s reload #重新載入配置
/usr/local/nginx/sbin/nginx -s quit #不知道,估計和stop差不多
/usr/local/nginx/sbin/nginx -s reopen #不知道,估計和reload類似
/usr/local/nginx/sbin/nginx -p /nginx/path #預設為/usr/local/nginx(nginx安裝路徑),修改後影響log目錄和html目錄
/usr/local/nginx/sbin/nginx -c /configure/file/path #配置檔案路徑,預設為conf/nginx.conf,有多個配置檔案時很有用,用這個可以啟動多個不同的nginx監聽不同埠
/usr/local/nginx/sbin/nginx -g #沒用過
配置
配置檔案在 安裝目錄/conf/nginx.conf
修改完後用/usr/local/nginx/sbin/nginx -s reload重新載入
#user nobody; #nginx啟動使用的使用者,配置fastcgi時,需要改為有許可權執行fastcgi的使用者
worker_processes 1; #nginx啟動的程序數,1個已經足夠了
#error_log logs/error.log; #nginx 錯誤日誌 相對於/usr/local/nginx/
#error_log logs/error.log notice; #nginx 記錄警告日誌 相對於/usr/local/nginx/ ,可改為logs/notice.log
#error_log logs/error.log info; #nginx 記錄資訊日誌 相對於/usr/local/nginx/ ,可改為logs/info.log
#pid logs/nginx.pid; #nginx程序檔案,最好不要改
events {
worker_connections 1024; #nginx最大連線數,最好小於系統的socket最大數和檔案開啟數
}
http {
include mime.types; #見同目錄mime.types,用於根據檔案字尾產生http header
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 0;
keepalive_timeout 65; #保持連線時間,單位:秒
#gzip on;
server {
listen 80; #監聽的埠
server_name localhost; #伺服器名
#charset koi8-r; #預設字符集
#access_log logs/host.access.log main; #根據訪問域名生成對應的訪問日誌
location / {
root html; #根目錄,相對於安裝目錄
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;
}
# 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; #轉發到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;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}