#CentOS + Nginx 的常用操作指令总结

时间:2019-11-25
本文章向大家介绍#CentOS + Nginx 的常用操作指令总结,主要包括#CentOS + Nginx 的常用操作指令总结使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

CentOS + Nginx 的常用操作指令总结

一. 关于CentOS

  1. 查看 yum 源是否存在
    yum list | grep nginx
  2. 如果不存在 或者 不是自己想要的版本 可以自己设置Nginx的源
    • 用vim 打开 (没有会自己创建)
    vim /etc/yum.repos.d/nginx.repo
    • 写入如下代码 (官方提供的放心用)
     [nginx]
     name=nginx repo
     baseurl=http://nginx.org/packages/OS/OSRELEASE/$basearch/
     gpgcheck=0
     enabled=1
    • 完成后,你需要修改一下对应的操作系统和版本号,因为我的是centos和7的版本,所以改为这样
    baseurl=http://nginx.org/packages/centos/7/$basearch/
  3. 一切就绪 安装 Nginx
    yum install nginx
  4. 查看 Nginx 的版本
    Nginx -v
  5. 启动服务
    systemctl start nginx.service
  6. 关闭服务
    systemctl stop nginx.service
  7. 重启
    systemctl restart nginx.service
  8. 查看服务的启动状态
    ps aux | grep nginx
  9. 查看端口占用情况
    netstat -tlnp

    一. 关于Nginx

  10. Nginx 的启动 (在CentOS7.4版本里(低版本是不行的),是可以直接直接使用nginx启动服务的)
    nginx
  11. Nginx 文件夹
    • nginx.conf 文件是Nginx总配置文件,在我们搭建服务器时经常调整的文件。

    • 进入etc/nginx目录下,然后用vim进行打开
    cd /etc/nginx
    vim nginx.conf

    注意: # 是注释 和js不一样 不是 key: value的配置 直接空格就可以
    ```

    user nginx; #运行用户,默认即是nginx,可以不进行设置
    #Nginx进程,一般设置为和CPU核数一样
    worker_processes 1;
    #错误日志存放目录
    error_log /var/log/nginx/error.log warn;
    #进程pid存放位置
    pid /var/run/nginx.pid;

    events {
    worker_connections 1024; # 单个后台进程的最大并发数
    }

    http {
    include /etc/nginx/mime.types; #文件扩展名与类型映射表
    default_type application/octet-stream; #默认文件类型
    #设置日志模式
    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;   #nginx访问日志存放位置
    
     sendfile        on;   #开启高效传输模式
     #tcp_nopush     on;    #减少网络报文段的数量
    
     keepalive_timeout  65;  #保持连接的时间,也叫超时时间
    
     #gzip  on;  #开启gzip压缩
    
     include /etc/nginx/conf.d/*.conf; #包含的子配置项位置和文件
    
    
    接最后一行  进入conf.d目录,然后使用vim default.conf进行查看。
    
    

    server {
    listen 80; #配置监听端口
    server_name localhost; //配置域名

     #charset koi8-r;     
     #access_log  /var/log/nginx/host.access.log  main;
    
     location / {
         root   /usr/share/nginx/html;     #服务默认启动目录
         index  index.html index.htm;    #默认访问文件
     }
    
     #error_page  404              /404.html;   # 配置404页面
    
     # redirect server error pages to the static page /50x.html
     #
     error_page   500 502 503 504  /50x.html;   #错误状态码的显示页面,配置后需要重启
     location = /50x.html {
         root   /usr/share/nginx/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;
     #}

    }

    ```

原文地址:https://www.cnblogs.com/var-chu/p/11926695.html