# Nginx配置管理
[TOC]
初始化的nginx默認主配置
執行 `grep -Evi "#|^$" nginx.conf` 得到如下結果!
~~~
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
~~~
## nginx的配置段
### 全局配置區
`worker_processes 1;` 指有一個工作的子進程,可以自行修改,因為要爭CPU資源,一般設置為CPU*核數。
### Event配置區
一般是配置Nginx連接的特性
例如:一個worker能同時允許產生多少個連接
` worker_connections 1024;` 配置值的時候需要結合系統參數。
### http服務器配置區
#### server虛擬主機配置區
>[danger] **提 示 :**如果未明確配置服務器IP地址訪問配置,將使用第一個server配置。
##### 基于域名的虛擬主機
使用域名`www.domain.com`為例!
~~~
server {
listen 80;
server_name www.domain.com;
error_page 404 /404.html;
location / {
root html/www;
index index.html;
access_log logs/www.domain.com_access.log main;
error_log logs/www.domain.com_error.log;
}
location ~ .+\.php.*$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
~~~
* * * * *
##### 基于端口的虛擬主機
以端口8080為例,域名依然使用`www.domain.com`
~~~
server {
listen 8080;
server_name www.domain.com;
error_page 404 /404.html;
location / {
root html/port;
index index.html;
access_log logs/www.domain.com_access.log main;
error_log logs/www.domain.com_error.log;
}
location ~ .+\.php.*$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
~~~
* * * * *
##### 基于IP的虛擬主機
以`192.168.0.200`為默認Ip配置虛擬主機
~~~
server {
listen 80;
server_name 192.168.0.200;
error_page 404 /404.html;
location / {
root html/ipvhosts;
index index.html;
access_log logs/192.168.0.200_access.log mian;
error_log logs/192.168.0.200_error.log;
}
location ~ .+\.php.*$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
~~~