nginx命令及配置

108 阅读2分钟
  • 利用linux 的yum安装

 yum install nginx
 nginx -v
  • 查看安装后所所涉及的目录

rpm -ql nginx
  • nginx.conf文件编辑

利用vscode的ssl功能编辑linux服务器上的文件

  • 运行用户,默认即是nginx,可以不进行设置user nginx;

  • Nginx进程,一般设置为和CPU核数一样

worker_processes 1;

  • 错误日志存放目录

error_log /var/log/nginx/error.log warn;

  • 进程pid存放位置,并修改

pid        /var/run/nginx.pid;
events {
    worker_connections  1024; # 单个后台进程的最大并发数
}
  • conf.d目录default.conf 配置项

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目录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;
    #}
}


 修改项目路径
    location / {
        root   /mydisk/www;     #服务默认启动目录
        index  index.html index.htm;    #默认访问文件
    }

配置ssl 使用https访问,配置过程 需要重新加载配置文件 nginx -s reload
    listen       443   ssl;
    server_name  localhost; #你们的域名,如www.abc.com;
    ssl_certificate      /opt/https_certificate/pems.pem;#证书路径
    ssl_certificate_key  /opt/https_certificate/keys.key;#证书秘钥路径
    ssl_session_timeout  5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;#按照这个加密套件配
   


配置404
 error_page  404              /404.html;   # 配置404页面



命令
systemctl start nginx.service
nginx  -s stop 、 nginx -s quit 、killall nginx 、systemctl stop nginx.service
nginx -s reload
查询服务的运行状况
ps aux | grep nginx


反向代理
在默认配置里,使用ssl https默认的443端口,代理访问本地的端口。服务器只需要开通一个443端口即可
 default.conf 
 location / {
        proxy_pass http://localhost:8000;
    }


demo.conf
server{  
    listen   8000   ;   #配置监听端口
    # server_name  localhost; #配置域名
    location  / {
        root   /mydisk/www;     #服务默认启动目录
        index  index.html index.htm;    #默认访问文件
    }
    error_page  404              /404.html;   # 配置404页面
    error_page   500 502 503 504  /50x.html; 
}


利用$http_user_agent 获取当前客户端类型 ,决定走哪个项目路径。
注意 if 后面必须要有空格 
if ($http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry)'){
            root /mydisk/www/mobile;
        }