- 1.nginx.conf主要由三部分组成
- user nobody;
- error_log logs/error.log;
- error_log logs/error.log notice;
- error_log logs/error.log info;
- pid logs/nginx.pid;
- 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;
- tcp_nopush on;
- keepalive_timeout 0;
- gzip on;
- 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;
- }
- }
1.nginx.conf主要由三部分组成
- 全局块:从配置文件开始到 events 块之间的内容,主要会设置一些影响nginx 服务器整体运行的配置指令,主要包括配置运行 Nginx 服务器的用户(组)、允许生成的 worker process 数,进程 PID 存放路径、日志存放路径和类型以及配置文件的引入等。
- events块:events 块涉及的指令主要影响 Nginx 服务器与用户的网络连接,常用的设置包括是否开启对多 work process 下的网络连接进行序列化,是否允许同时接收多个网络连接,选取哪种事件驱动模型来处理连接请求,每个 word process 可以同时支持的最大连接数等。 这部分的配置对 Nginx 的性能影响较大,在实际中应该灵活配置。
- http块:算是 Nginx 服务器配置中最频繁的部分,代理、缓存和日志定义等绝大多数功能和第三方模块的配置都在这里。http 块也可以包括 http全局块、server 块。
```nginx
user nobody;
worker_processes 2; //开启的nginx进程数量,通常是随cpu的核心数一致
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;
pid logs/nginx.pid;
events { //events 块 worker_connections 50000; // 每个nginx进程支持的并发访问量。 }
http { //http块 include 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 on;
tcp_nopush on;
keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
upstream web {
server 192.168.99.100:80;server 192.168.99.200:80;
} server { listen 80; server_name localhost;
#charset koi8-r;#access_log logs/host.access.log main;location / {proxy_pass http://web;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;# 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;
}
}
}
```
