慕课学习之WebServer安装配置

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扩展知识

  1. 虚拟主机(一个server就是一个虚拟主机)
    /etc/nginx/conf.d/imooc.conf
server {
        listen 80;
        server_name www.imooc.test;
        root /data/www;
        index index.html index.htm;
}
  1. 多域名、多端口
server {
        listen 80;
        listen 9999; #多端口
        server_name www.imooc.test www.imooc3.test; #多域名
        root /data/www;
        index index.html index.htm;
}
  1. 伪静态(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;      #伪静态
 }
}
  1. 格式化日志
    /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;
  2. 反向代理和负载均衡
    反向代理: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;      #负载均衡、反向代理
  }
}
  1. 调试技巧
    打印请求地址
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状态码或许不应该认为是一种错误,而是对客户端有缓存情况下服务端的一种响应。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容