Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,处理请求是异步非阻塞的,多个连接(万级别)可以对应一个进程。而Apache是同步多进程模型,一个连接对应一个进程。
#user nobody;
#指定nginx进程数
worker_processes 1;
#全局错误日志及PID文件
#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服务器,利用它的反向代理功能提供负载均衡支持
http {
#设定mime类型,类型由mime.type文件定义
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 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,
sendfile on;
#tcp_nopush on;
#连接超时时间
#keepalive_timeout 0;
keepalive_timeout 65;
#开启gzip压缩
#gzip on;
#设定负载均衡的服务器列表 支持多组的负载均衡,可以配置多个upstream 来服务于不同的Server.
#nginx 的 upstream 支持 几 种方式的分配
#1)、轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
#2)、weight 指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。 跟上面样,指定了权重。
#3)、ip_hash 每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
#4)、fair
#5)、url_hash #Urlhash
upstream mysvr {
#weigth参数表示权值,权值越高被分配到的几率越大
#1.down 表示单前的server暂时不参与负载
#2.weight 默认为1.weight越大,负载的权重就越大。
#3.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
#server 192.168.1.116 down;
#server 192.168.1.116 backup;
server 192.168.1.121 weight=1;
server 192.168.1.122 weight=2;
}
#配置代理服务器的地址,即Nginx安装的服务器地址、监听端口、默认地址
server {
#1.侦听80端口
listen 80;
#对于server_name,如果需要将多个域名的请求进行反向代理,可以配置多个server_name来满足要求
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
# 默认主页目录在nginx安装目录的html子目录。
root html;
index index.html index.htm;
proxy_pass http://mysvr; #跟载均衡服务器的upstream对应
}
#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;
# }
#}
}注意:
1、文件目录不能出现中文
2、 tasklist /fi "imagename eq nginx.exe" 查看是否启动成功
3、Nginx优化
1. gzip压缩优化
2. expires缓存有还
3. 网络IO事件模型优化
4. 隐藏软件名称和版本号
5. 防盗链优化
6. 禁止恶意域名解析
7. 禁止通过IP地址访问网站
8. HTTP请求方法优化
9. 防DOS攻击单IP并发连接的控制,与连接速率控制
10. 严格设置web站点目录的权限
11. 将nginx进程以及站点运行于监牢模式
12. 通过robot协议以及HTTP_USER_AGENT防爬虫优化
13. 配置错误页面根据错误码指定网页反馈给用户
14. nginx日志相关优化访问日志切割轮询,不记录指定元素日志、最小化日志目录权限
15. 限制上传到资源目录的程序被访问,防止木马入侵系统破坏文件
16. FastCGI参数buffer和cache配置文件的优化
17. php.ini和php-fpm.conf配置文件的优化
18. 有关web服务的Linux内核方面深度优化(网络连接、IO、内存等)
19. nginx加密传输优化(SSL)
20. web服务器磁盘挂载及网络文件系统的优化
21. 使用nginx cache4、Nginx+Keepalive负载均衡
















