zoukankan      html  css  js  c++  java
  • nginx学习四:基础配置

    访问nginx首页

    1. 确保nginx已安装

    2. 启动nginx

      systemctl start nginx.service
      
    3. 查看nginx

      # 查看nginx pid
      cat /var/run/nginx.pid
      # 查看nginx进程
      ps -aux | grep nginx
      # 查看网络状态
      netstat -nltp
      
    4. 发现浏览器访问 192.168.190.58:80 访问不到nginx首页问题

      # 查看nginx状态
      systemctl status nginx
      # nginx检查
      nginx -t
      # 查看nginx服务是否启动
      ps aux|grep nginx
      # 查看 80 端口是否分配给了nginx
      netstat -nltp
      # 80端口可能没开放,对80端口进行防火墙配置
      firewall-cmd --zone=public --add-port=80/tcp --permanent
      –zone #作用域
      –add-port=80/tcp #添加端口,格式为:端口/通讯协议
      –permanent #永久生效,没有此参数重启后失效
      # 重启防火墙
      systemctl restart firewalld.service
      
      # 再次查看nginx页面,发现成功
      
      # 查看已经开放的端口
      firewall-cmd --list-ports
      

    nginx配置详解

    • CoreModule 核心模块,全局配置

      CoreModule层下可以有Event、Http

    • EventModule 事件驱动模块

    • HttpCoreModule http内核模块

      HTTP模块层允许有多个Server层,Server主要用于配置多个网站。

      Server层又允许有多个Location,Location主要用于定义网站访问路径

      # 公共的配置定义在 http{}
      http {  // http层开始
      		//使用 server 配置网站,每个 server{} 代表一个网站(简称虚拟主机)
      		server {
      				listen  80;  //监听端口,默认80
      				server_name  localhost;  //提供服务的域名或主机名
      				access_log host.access.log  //访问日志
      				//控制网站访问路径
      				location / {
      					 root /usr/share/nginx/html;  //存放网站代码路径
      					 index  index.php  index.html  index.htm;   //服务器返回的默认页面文件
      				}
      				//指定错误代码,统一定义错误页面,错误代码重定向到新的location
      				error_page  500 502 503 504 /50x.html
      		}
      }
      

    /etc/nginx/nginx.confg详解

    # CoreModule核心模块
    user  nginx;   #nginx进程所使用的的用户
    worker_processes  1;  # nginx运行的work进程数量(建议与CPU数量一致,或 auto)
    error_log  /var/log/nginx/error.log warn;  # 错误日志存放处
    pid        /var/run/nginx.pid;  # nginx服务运行后产生的pid进程号
    
    # events事件模块
    events {
        worker_connections  1024;  # 每个worker进程支持的最大连接数
        use epoll;   # 事件驱动模型,默认epoll
    }
    
    
    # http模块
    http {
        include       /etc/nginx/mime.types;
        default_type  application/octet-stream;
    cc
        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  /var/log/nginx/access.log  main;
    
        sendfile        on;
        #tcp_nopush     on;
    
        keepalive_timeout  65;
    
        #gzip  on;
    
        include /etc/nginx/conf.d/*.conf;   //重要
    }
    

    过滤掉注释和空行

    [root@localhost conf.d]# egrep -v '^$|^.*#' default.conf
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    }
    

    Nginx配置网站

    目的:当我们访问 game.oldboy.com的时候访问 /oldboy_code/ 里面的页面代码

    1、在 /etc/nginx/conf.d/ 下新建配置文件,要求一定是 .conf 结尾,然后将默认配置文件关闭,注释

    cd /etc/nginx/conf.d
    mv default.conf default.off
    vim game.conf
    # game.conf
    server {
    	listen 80;
    	server_name game.oldboy.com;
    	location / {
    		root /oldboy_code;
    		index index.html;
    	}
    }
    
    # nginx -t 做语法测试
    [root@localhost conf.d]# nginx -t
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
    

    2、获取网页代码

    # mkdir -p /oldboy_code/(www,bbs,blog)
    mkdir -p /oldboy_code/
    vim index.html
    

    3、启动或重载nginx

    systemctl start nginx
    systemctl reload nginx (建议使用)
    nginx -s reload 
    

    4、nginx访问出现 403 的问题

    # 查看 log 日志
    cat /etc/log/nginx/error.log
    2020/03/30 01:08:24 [error] 5357#5357: *22 "/oldboy_code/index.html" is forbidden (13: Permission denied), client: 192.168.190.156, server: game.oldboy.com, request: "GET / HTTP/1.1", host: "192.168.190.58"
    原因:
    1、由于启动用户和nginx工作用户不一致,将nginx.conf的user改为和启动用户一致
    vim /etc/nginx/nginx.conf
    	user=root
    nginx -t
    systemctl reload nginx
    
    2、缺少 index.html 等指定的文件
    3、权限问题
    修改web目录的读写权限,或者把nginx的启动用户改成目录的所属用户,重启nginx
    chmod -R 755 /var/www
    4、SELinux设置为开启状态(enabled)的原因
    首先查看本机SELinux的开启状态,如果SELinux status 参数为enabled即为开启状态
    sestatus -v
    临时关闭(不用重启)
    setenforce 0
    永久关闭(需要重启)
    修改配置文件 /etc/selinux/config
    vi /etc/selinux/config
       #SELINUX=enforcing
       SELINUX=disabled
    reboot生效
    

    5、通过域名访问,要修改本地 hosts 文件

    cat /etc/hosts
    192.168.190.58 game.oldboy.com
    

    排错

    1. 域名解析问题

    2. nginx无法启动

      1. 是否端口被占用

        lsof -i:80
        
      2. 配置文件写错了

    3. 重载失败

      1. 配置文件写错了

        就算配置文件写错了,重载也不一定失败,一定要 nginx -t 检测。

    4. 文件路径写错

      1. nginx -t 不报错,systemctl reload nginx 不报错

      2. 访问页面 404

      3. 一定要查看日志信息

        tail /var/log/nginx/error.log
        
    5. root访问页面 index.html 改成 index.htm

      1. 出现 403 forbidden 问题

        mv index.html index.htm
        
  • 相关阅读:
    UVA 12545 Bits Equalizer
    UVA 1610 Party Games
    UVA 1149 Bin Packing
    UVA 1607 Gates
    UVA 12627 Erratic Expansion
    UVA10562-Undraw the Trees(递归)
    UVA10129-Play on Words(欧拉路径)
    UVA816-Abbott's Revenge(搜索进阶)
    UVA1103-Ancient Messages(脑洞+dfs)
    UVA839-Not so Mobile
  • 原文地址:https://www.cnblogs.com/KbMan/p/14339435.html
Copyright © 2011-2022 走看看