对于centos(linux)下使用nginx的总结

    1. 安装nginx
      执行yum install nginx
    1. 查看nginx的文件
      执行 rpm -ql nginx
    1. 常用的配置和文件有
      nginx/conf.d/default.conf
      nginx/nginx.conf (下面的配置都是在此文件)
    1. nginx启动
      执行nginx或执行systemctl start nginx.service
      检查是否启动成功执行ps aux | grep nginx;成功一般有3条记录
    1. nginx停止
      立即:nginx -s stop
      从容:nginx -s quit
  • 6.nginx重启/重载
    执行systemctl restart nginx.service
    重载 nginx -s reload
  • 7.访问控制
 location / {
        deny   123.9.51.42;
        allow  232,123,123,2;
    }
=号代表精确匹配
location =/img{
        allow all;
    }
正则匹配
location ~\.php$ {
        deny all;
    }
  1. 配置虚拟主机
    虚拟主机就是可以在服务器上配置多个域名;一般通过设置端口号来实现;
server{
        listen 8001;
        server_name localhost;
        root /usr/share/nginx/html/html8001;
        index index.html;
}

  1. 反向代理
server{
        listen 80;
        server_name test12.com;
        location / {
               proxy_pass http://test34.com;
        }
}
  1. nginx判断移动和pc
server{
        listen 80;
        server_name test.com;
        location / {
         root /usr/share/nginx/pc;
         if ($http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry)') {
            root /usr/share/nginx/mobile;
         }
         index index.html;
        }
}
  1. nginx 开启gzip压缩
http {
   .....
    gzip on;
    gzip_types text/plain application/javascript text/css;
   .....
}
  1. nginx 配置跨域
location /apis { #添加访问目录为/apis的代理配置
            rewrite  ^/apis/(.*)$ /$1 break;
            proxy_pass   http://localhost:82;
 }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。