WebServer安装配置
Apache
- 安装:yum install httpd
- 启动:service httpd start
检查监听端口:netstat -anpl|grep http - 停止:service httpd stop
- 虚拟主机
配置多域名的时候使用 /etc/httpd/conf/httpd.conf
<VirtualHost *:80>
ServerName www.imooc.test
DocumentRoot "/data/www"
<Directory "/data/www">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
注意!!!如果不生效的话:关闭selinux,命令setenforce 0
报错日志:because search permissions are missing on a component of the path
- 伪静态
/etc/httpd/conf/http.conf
LoadModule rewrite_module modules/mod_rewrite.so #加载rewrite模块
<VirtualHost *:80>
ServerName www.imooc.test
DocumentRoot "/data/www"
<Directory "/data/www/">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*).htmp$ index.html #伪静态规则
</IfModule>
</Directory>
</VirtualHost>
配置的伪静态规则说明:所有的请求都发往index.html这个页面
Nginx
- 安装:yum install nginx
- 启动:service nginx start
- 停止:service nginx stop
- 重载:service nginx reload
Nginx扩展知识
- 虚拟主机(一个server就是一个虚拟主机)
/etc/nginx/conf.d/imooc.conf
server {
listen 80;
server_name www.imooc.test;
root /data/www;
index index.html index.htm;
}
- 多域名、多端口
server {
listen 80;
listen 9999; #多端口
server_name www.imooc.test www.imooc3.test; #多域名
root /data/www;
index index.html index.htm;
}
- 伪静态(nginx默认开启)
server {
listen 80;
listen 9999; #多端口
server_name www.imooc.test www.imooc3.test; #多域名
root /data/www;
index index.html index.htm;
location / {
rewrite ^(.*)\.htmp$ /index.html; #伪静态
}
}
- 格式化日志
/etc/nginx/nginx.conf
log_format imooc '\$remote_addr-"$http_user_agent"';
虚拟主机配置日志路径 /etc/nginx/conf.d/imooc.conf
access_log /var/log/nginx/access_imooc.log imooc;
- 反向代理和负载均衡
反向代理:nginx+web应用程序
负载均衡:后端多机器进行负载
upstream imooc_hosts{
server ip1:port weight=5; #负载均衡、反向代理
server ip2:port weight=1; #负载均衡、反向代理
}
server {
listen 80;
listen 9999; #多端口
server_name www.imooc.test www.imooc3.test; #多域名
root /data/www;
index index.html index.htm;
location / {
\#rewrite ^(.*)\.htmp$ /index.html;
proxy_set_header Host www.54php.cn;
proxy_pass http://imooc_hosts; #负载均衡、反向代理
}
}
- 调试技巧
打印请求地址
server {
listen 80;
listen 9999; #多端口
add_header Content-Type "text/plain;charset=utf-8";
return 200 "$http_host"; #页面返回浏览器中输入的地址
server_name www.imooc.test www.imooc3.test; #多域名
root /data/www;
index index.html index.htm;
location / {
\#rewrite ^(.*)\.htmp$ /index.html;
proxy_set_header Host www.54php.cn;
proxy_pass http://imooc_hosts; #负载均衡、反向代理
}
}
HTTP 304状态码的详细讲解
- 304状态码或许不应该认为是一种错误,而是对客户端有缓存情况下服务端的一种响应。