nginx-主配置文件
nginx-主配置文件
- 一、主配置文件nginx.conf内容
- 二、修改配置的文件后的操作
- 三、配置虚拟主机的域名
- 1. 修改nignx.conf配置文件
- 2. 新建域名对应的网页根目录
- 3. 重载nginx配置
- 4. 验证
一、主配置文件nginx.conf内容
[root@web1 conf]# cat nginx.conf#user nobody; # nginx woker进程启动的时候会使用那个用户去启动,默认nobody
worker_processes 2; # 指定2个woker进程,一般情况下与cpu核心的个数一致
# auto 表示 自动匹配cpu核心数量#error_log logs/error.log; #错误日志存放的路径
#error_log logs/error.log notice; # 记录警告及以上级别错误
#error_log logs/error.log info;#pid logs/nginx.pid; #master进程的pid号#事件块
events {worker_connections 2048; #定义一个worker进程可以同时并发连接多少个请求 总的请求数=woker进程数*2048use epoll; # #定义nginx在进行大并发处理的时候采用epoll模型
}# Nginx 支持的 I/O 多路复用模型比较:select、poll 和 epoll
I/O 多路复用解决的大并发连接的时候,nginx如何去处理的问题http {include mime.types; # 申明nginx支持的媒体类型,可以传输识别哪些类型的文件default_type application/octet-stream; # 默认的发送的数据类型server_tokens off; # 隐藏nginx的版本号#log_format main '$remote_addr - $remote_user [$time_local] "$request" '# '$status $body_bytes_sent "$http_referer" '# '"$http_user_agent" "$http_x_forwarded_for"';log_format main 是日志记录格式的名字$remote_addr 是客户机http请求报文里的ip包里的源ip地址$remote_user 远程用户$time_local 访问的时间$request http请求报文里的url$status 状态码$body_bytes_sent http响应报文回复的数据大小$http_referer 本次访问是从哪里引流过来的 --》跳转$http_user_agent 客户端的用户代理浏览器$http_x_forwarded_for 记录从那个负载均衡器转发的#access_log logs/access.log main; #访问日志 main是格式的名字sendfile on; # # 启用零拷贝传输#tcp_nopush on;#keepalive_timeout 0;keepalive_timeout 65; #长连接 65秒gzip on; # 开启压缩功能,响应报文的body部分进行压缩#一个server对应nginx进程提供的网站服务,一个server对应一个网站 --》虚拟主机 对应一个网站server {listen 80; # 监听的端口#server_name localhost;server_name www.sc.com; # 网站的域名,默认localhost#charset koi8-r;#access_log logs/host.access.log main; #访问日志#定义路由--》根路径location / {root html; # 网页根目录 在html文件夹 html在nginx安装的路径下index index.html index.htm; # 定义网站的首页}#error_page 404 /404.html; # 出现404状态码,返回/404.html页面(需在网页根目录下)# redirect server error pages to the static page /50x.html#error_page 500 502 503 504 /50x.html; 设置50x错误指向的页面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;# }#}}
二、修改配置的文件后的操作
检测nginx.conf配置文件是否正确
[root@web-2 conf]#
nginx -t
nginx: the configuration file /usr/local/nginx1/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx1/conf/nginx.conf test is successful
重载配置
[root@web-2 conf]#
nginx -s reload
nginx: [emerg] unknown directive “sanchuang” in /usr/local/nginx1/conf/nginx.conf:4
三、配置虚拟主机的域名
1. 修改nignx.conf配置文件
server {listen 80;server_name www.feng.com;access_log logs/feng.com.access.log main;
error_log logs/feng.com.error.log;location / {root html/feng.com;index index.html index.htm;}error_page 404 /404.html;
增加两个域名则需再写一个server块
2. 新建域名对应的网页根目录
[root@web-2 conf]# cd /usr/local/nginx1/html/
[root@web-2 html]# mkdir feng.com
[root@web-2 html]# cd feng.com/
[root@web-2 feng.com]# vim index.html # 创建一个首页文件
[root@web-2 feng.com]# cat index.html
welcome to feng.com
3. 重载nginx配置
[root@web-2 feng.com]#
nginx -t
nginx: the configuration file /usr/local/nginx1/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx1/conf/nginx.conf test is successful
[root@web-2 feng.com]#nginx -s reload
4. 验证
去访问www.feng.com域名对应的网站
另一台linux系统里测试访问
修改linux系统里的/etc/hosts
文件,添加www.feng.com对应的ip地址
[root@nginx-1 fd]# vim /etc/hosts
192.168.100.157 www.feng.com
测试在hosts文件里添加的域名是否可以解析到对应的ip地址
[root@nginx-1 fd]# ping www.feng.com
PING www.feng.com (192.168.100.157) 56(84) 比特的数据。
64 比特,来自 www.feng.com (192.168.100.157): icmp_seq=1 ttl=64 时间=0.576 毫秒
64 比特,来自 www.feng.com (192.168.100.157): icmp_seq=2 ttl=64 时间=0.219 毫秒
^C
— www.feng.com ping 统计 —
已发送 2 个包, 已接收 2 个包, 0% packet loss, time 1039ms
rtt min/avg/max/mdev = 0.219/0.397/0.576/0.178 ms
linux访问www.feng.com
[root@nginx-1 fd]# curl www.feng.com
welcome to feng.com
windows系统里测试访问
修改C:\Windows\System32\drivers\etc\hosts
文件
使用记事本打开此文件,添加下面的域名解析记录
192.168.100.157 www.feng.com
打开cmd程序,进行测试
C:\Users\Administrator>ping www.feng.com 测试
正在 Ping www.feng.com [192.168.100.157] 具有 32 字节的数据:
来自 192.168.100.157 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.100.157 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.100.157 的回复: 字节=32 时间=1ms TTL=64
来自 192.168.100.157 的回复: 字节=32 时间<1ms TTL=64
192.168.100.157 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 0ms,最长 = 1ms,平均 = 0ms
用浏览器访问域名,可查看内容
用了基于域名的虚拟主机,应该使用域名去访问,如果使用ip访问的话,默认会访问第1个虚拟主机(网站)