server{} 包含在http{}內部,每一個server{}都是一個虛擬主機(站點)
以下為nginx.conf配置文件中server{ }部分的內容。
server {
listen 80; //監聽端口為80,可以自定義其他端口,也可以加上IP地址,如,listen 127.0.0.1:8080;
server_name localhost; //定義網站域名,可以寫多個,用空格分隔。
#charset koi8-r; //定義網站的字符集,一般不設置,而是在網頁代碼中設置。
#access_log logs/host.access.log main; //定義訪問日志,可以針對每一個server(即每一個站點)設置它們自己的訪問日志。
##在server{}里有很多location配置段
location / {
root html; //定義網站根目錄,目錄可以是相對路徑也可以是絕對路徑。
index index.html index.htm; //定義站點的默認頁。
}
#error_page 404 /404.html; //定義404頁面
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html; //當狀態碼為500、502、503、504時,則訪問50x.html
location = /50x.html {
root html; //定義50x.html所在路徑
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#定義訪問php腳本時,將會執行本location{}部分指令
#location ~ \.php$ {
# proxy_pass http://127.0.0.1; //proxy_pass后面指定要訪問的url鏈接,用proxy_pass實現代理。
#}
# 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服務器監聽端口與地址,支持兩種形式,1 IP:Port, 2 unix:/path/to/sockt
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; //定義SCRIPT_FILENAME變量,后面的路徑/scripts為上面的root指定的目錄
# include fastcgi_params; //引用prefix/conf/fastcgi_params文件,該文件定義了fastcgi相關的變量
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht { //訪問的url中,以/.ht開頭的,如,www.example.com/.htaccess,會被拒絕,返回403狀態碼。
# deny all; //這里的all指的是所有的請求。
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000; //監聽8000端口
# listen somename:8080; //指定ip:port
# server_name somename alias another.alias; //指定多個server_name
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl; //監聽443端口,即ssl
# server_name localhost;
### 以下為ssl相關配置
# ssl_certificate cert.pem; //指定pem文件路徑
# ssl_certificate_key cert.key; //指定key文件路徑
# ssl_session_cache shared:SSL:1m; //指定session cache大小
# ssl_session_timeout 5m; //指定session超時時間
# ssl_protocols TLSv1 TLSv1.1 TLSv1.2; //指定ssl協議
# ssl_ciphers HIGH:!aNULL:!MD5; //指定ssl算法
# ssl_prefer_server_ciphers on; //優先采取服務器算法
# location / {
# root html;
# index index.html index.htm;
# }
#}

