一、概述
Nginx 专为 性能优化而开发,其最知名的优点是它的稳定性和低系统资源消耗,以及对并发连接的高处理能力(单台物理服务器可支持 30000-50000 个并发连接,tomcat 通常是几百,不超过 1000 个), 是一个高性能的 HTTP 和反向代理服务器,也是一个IMAP/POP3/SMTP 代理服务器。
二、Nginx功能
正向代理与反向代理
使用场景,比如说主机A192.168.1.1不能访问外网,而B192.168.1.2这台机器能够访问外网。
内网机器如果想上互联网,则只能通过外网机器代理实现。
A --> B(nginx正向代理8080) --> baidu.com
cd /usr/local/nginx/conf/
cp nginx.conf{,.bak}
vim nginx.conf
server {
listen 8080;
server_name localhost;
resolver 114.114.114.114 ipv6=off;
proxy_connect;
proxy_connect_allow 443 80;
proxy_connect_connect_timeout 10s;
proxy_connect_read_timeout 10s;
#proxy_coneect_send_timeout 10s;
location / {
proxy_pass $scheme://$http_host$request_uri;
}
}
反向代理场景,比如负载均衡,当用户访问时,解析到不用的业务服务器。
三、nginx.conf详解
#user nobody; // worker 进程工作数
worker_processes 1;
events {
// linux 默认使用epoll
worker_connections 1024; //每个 worker 进程允许最大打开的文件数
}
// 针对 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; // 和 sendfile 一起使用,当请求的数据包累计到了一定大小的时候再发送
#keepalive_timeout 0;
keepalive_timeout 65; // 客户端连接服务器的超时时间,默认65 秒,0 代表不保持连接。
#gzip on; // 一定打开,利于文件和请求数据的传输,同时消耗时间。
// 服务虚拟主机配置
server {
listen 80; // 监听端口
server_name localhost; // 监听服务器 ip、域名、或者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;
}
# 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;
# }
#}
}