核心配置文件
#user nobody;
worker_processes 1; # 默认为1,表示开启一个业务进程
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024; #单个业务进程可接受连接数
}
http {
include mime.types; #引入http mime类型
default_type application/octet-stream; #如果mime类型没匹配上,默认使用二进制流的方式传输。
#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;#使用linux的 sendfile(socket, file, len) 高效网络传输,也就是数据0拷贝。
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {#这个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; #在html目录下找/50x.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;
# }
#}
}
nginx反向代理的好处
- 提高访问速度 (nginx会有缓存)
- 进行负载均衡
- 保证后端服务安全 (后端服务不直接暴露到互联网,只运行在局域网中,通过nginx进入内网访问)
nginx配置