[root@ansible ~]# whereis nginx #查看nginx位置
nginx: /usr/local/nginx
[root@ansible ~]# cd /usr/local/nginx
[root@ansible nginx]# ls
client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp ssl uwsgi_temp
conf目錄爲配置文件目錄,html存放網頁文件,logs存放日誌文件,sbin存放二進制nginx程序,ssl存放https密鑰文件
[root@ansible nginx]# cat conf/nginx.conf | grep -v '^$' #查看配置文件,過濾掉空行
#user nobody; #運行用戶
worker_processes 1; #啓動進程,通常和CPU數相等
#全局錯誤日誌
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid; #pid文件
#工作模式及連接數上限
events {
worker_connections 1024; #單個後臺worker_process進程的最大併發連接數
}
http {
include mime.types; #設定mime類型,類型由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 指令指定 nginx 是否調用 sendfile 函數(zero copy 方式)來輸出文件,
#對於普通應用,必須設爲 on,
#如果用來進行下載等應用磁盤IO重負載應用,可設置爲 off,
#以平衡磁盤與網絡I/O處理速度,降低系統的uptime.
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0; #連接超時時間
keepalive_timeout 65;
#gzip on; #開啓gzip壓縮
#虛擬主機配置
server {
listen 80; 偵聽80端口
server_name localhost; #虛擬主機名稱
#charset koi8-r;
#access_log logs/host.access.log main; 虛擬主機的訪問日誌
location / {
root html; #虛擬主機根目錄
index index.html index.htm; #默認首頁索引文件
}
#查看nginx狀態的模塊
location /nginx_status {
stub_status on;
access_log off;
allow all;允許的ip
#deny all;拒絕的ip
}
#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
#
#PHP 腳本請求全部轉發到 FastCGI處理. 使用FastCGI默認配置.
#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
#
#https虛擬主機配置
server {
listen 443 ssl;
server_name 192.168.29.9;
證書和密鑰存放位置,自定義的
ssl_certificate /usr/local/nginx/ssl/nginx.crt;
ssl_certificate_key /usr/local/nginx/ssl/nginx.key;
# keepalive_timeout 70;
# server_tokens off;
# fastcgi_param HTTPS on;
# fastcgi_param HTTPS_SCHEME https;
# 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;
}
}
}
nginx相關命令
驗證配置是否正確: nginx -t
[root@ansible nginx]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
查看Nginx的版本號:nginx -V
[root@ansible nginx]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.8.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: –prefix=/usr/local/nginx –with-http_stub_status_module –with-http_ssl_module
啓動Nginx:nginx或nginx -c
[root@ansible sbin]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf #若不用-c指定配置文件,則以默認配置啓動
快速停止或關閉Nginx:nginx -s stop
[root@ansible sbin]# /usr/local/nginx/sbin/nginx -s stop
正常停止或關閉Nginx:nginx -s quit
[root@ansible sbin]# /usr/local/nginx/sbin/nginx -s quit
配置文件修改重裝載命令:nginx -s reload
[root@ansible sbin]# /usr/local/nginx/sbin/nginx -s reload
重新打開日誌文件:nginx -s reopen
[root@ansible sbin]# /usr/local/nginx/sbin/nginx -s reopen
打開幫助信息:nginx -h
[root@ansible sbin]# /usr/local/nginx/sbin/nginx -h
nginx version: nginx/1.8.1
Usage: nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /usr/local/nginx/)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file