参考
《Nginx高性能Web服务器详解》
第二章:配置文件介绍,基础配置指令介绍。
第三章:模块化架构介绍。
第四章:比较高级的配置指令
第五章:Gzip压缩功能的配置
第六章:Rewrite功能的配置
第七章:正向代理、反向代理、负载均衡的配置
第八章:Web缓存功能的配置
第九章:邮件服务功能配置。
配置文件语法
1)Nginx配置文件中,每条指令配置都必须以分号结束。
2)“#”后边的内容是注释。
3)分块配置,常见的块由http块、server块、location块、upstream块、mail块等。一个块代表一个作用域,作用域存在嵌套。
默认配置文件
### 全局块 开始 ### #配置运行Nginx服务器用户和用户组 #将此指令行注释掉,则所有用户都可以启动Nginx进程 #user nobody; #允许生成的worker process数 worker_processes 1; #配置错误日志的存放路径,及日志的级别(可选) #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #Nginx进程PID存放路径 #pid logs/nginx.pid; ### 全局块 结束 ### ### events块 开始 ### events { #配置最大连接数(设置允许每个worker process同时开启的最大连接数(包括所有可能的连接数)) worker_connections 1024; } ### events块 结束 ### ### http块 开始 ### http { #引入配置文件,此处使用的是相对路径。mime.types用于识别前端请求的资源类型。 include mime.types; #用于处理前端请求的MIME类型 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方式传输文件 sendfile on; #tcp_nopush on; #连接超时时间 #keepalive_timeout 0; keepalive_timeout 65; #gzip on; ### server块 开始 ### #配置虚拟主机 localhost server { #配置监听端口和主机名称 listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; #配置处理 / 请求的location location / { #配置请求的根目录 root html; #设置网站的默认首页。 index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # #设置网站的错误页面 #错误代码 错误页面的路径(Nginx安装目录下的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; # } #} } ### http块 结束 ###
修改配置文件后,平滑重启使配置文件生效
sudo ./sbin/nginx -s reload
例:配置一个虚拟主机
1)在nginx.conf中的http块中加入一下代码
server { listen 8082; #基于IP的虚拟主机配置 server_name 192.168.1.31; #服务日志存放目录,目录不以“/”开始,它是一个相对路径,nginx安装目录为根目录。 access_log myweb/server2/log/access.log; error_page 404 /404.html; #设置网站错误页面(404代表无法找到网页) #配置处理/server2/location1请求的location location /server2/location1 { root myweb; #请求的根目录 index svr2-loc1.html; #网站的默认首页 } location /svr2/loc2 { alias myweb/server2/location2; #对location的URI进行更改,后面的是修改后的根路径 index svr2-loc2.html; } location = /404.html { #配置错误页面转向 root myweb; index 404.html; } }
2)用ifconfig工具为同一块网卡添加一个IP别名。
1.ifconfig:打印网络配置信息(我的机器上,使用中的网卡名为eth0)
2.为eth0添加一个IP别名192.168.1.31,用于Nginx服务器提供的虚拟主机。(up表示立即启用此别名)
ifconfig eth0:0 192.168.1.31 netmask 255.255.255.0 up
3.使用ifconfig查看是否配置正确。
3)在nginx安装目录下新建一个myweb文件夹,安书上的目录结构放置网页。
4)在浏览器中测试