nginx配置及常用命令总结

vim nginx.conf 进入到nginx配置
可以看到一段server代码块,如下代码,集体含义请看我添加的注释

 server {
        listen       80;    #监听80端口
        server_name  localhost;   #请求时ip

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;  //存放html文件地址  root代表是一个相对路径  html是一个文件夹
            index  index.html index.htm;  //默认打开的网页,存放于上面html文件夹下
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;  #错误页面以及错误页面存放位置
        location = /50x.html {
            root   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;
        #}
    }

修改了nginx.conf配置文件,还需要进入sbin下,只想./nginx -s reload 重新加载才能生效
输入ps -ef|grep nginx 查看nginx进程,可以看到有两个进程,一个主进程,一个工作进程
在这里插入图片描述
工作线程数量也是可以修改的,同样vim nginx.conf 进入到nginx配置,在文件开头就能看到worker_processes 1,
修改后面数字就可以改变工作线程数量(ngxin -t可以检测修改配置文件是否有问题)

在这里插入图片描述
master进程管理worker进程,master可以接收外界传递进来信号,然后传递给worke去处理
master监控worker,如果worker发生异常,master还会再启动一个worker去处理任务

在这里插入图片描述
nginx.conf 配置文件

events {
	#use epoll;这个可不加,默认是使用epoll
    worker_connections  1024;  #设定每个工作线程可允许连接的最大连接数
}

在这里插入图片描述

#user  nobody;  执行进程的用户名
worker_processes  1; #工作线程数量,有几个cpu可设置几个,如果服务器还有其他软件运行,可设置n-1个

#错误日志  日志级别 debug info notice warn error crit  从低到高
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}

http {
	# 在nginx.conf同级目录下,有一个mime.types,里面包含的是请求类型(如text/html,image/gif等)
    include       mime.types;  
    #默认type类型
    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  logs/access.log  main;
	
	#文件高效传输,默认打开
    sendfile        on;
    #数据包累积到一定大小再去发送,若要开启此功能,sendfile也必须开启
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;客户端连接服务器超时时间 单位为秒
	# gzip压缩  提高传输效率  节省带宽
    #gzip  on; 
    #限制最小压缩,小于1字节文件不会压缩
    #gzip_min_length  1;
    #gzip_comp_level   3     设置压缩比    可设置范围1-9   值越大  cpu占用越大
	#gizp_types text/xml  第一压缩文件类型
#server可配置多个,也可以以文件形式导入server   include  文件名.conf   导入server
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
		
#允许跨域请求的域,*代表所有
add_header 'Access-Control-Allow-Origin'  *;
#允许带上cookie请求
add_header  'Access-Control-Allow-Credentials'  'true' ;
#允许请求的方法,比如GET/POST/PUT/DELETE
add_header 'Access-Control-Allow-Methods'  *;
#允许请求的header
add_header  'Access-Control-Allow-Headers'  *;

#对源站点验证  防盗链
valid_referers  *.mk.com;
#非法引入会进入下方判断
if($invalid_referer) {
	return
	404;
}
        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   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;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
        #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

在这里插入图片描述
第一个location为项目位置,index为项目首页
第二个location为项目静态资源位置在这里插入图片描述
alias 别名资源路径 /static 别名网络路径

nginx打开失败以及失效解决方法

在这里插入图片描述可能是目录/var/run/nginx/不存在,也可能是nginx.pid文件不存在
目录不存在了重新创建一个

nginx.pid文件不存在,执行以下命令,重新手动指定一个ngin.conf即可
在这里插入图片描述

要使用nginx命令,需要进入到sbin目录下
./nginx -s stop 快速停止nginx
./nginx -s quit 用户连接都关闭后才会停止
./nginx -t 检查配置文件语法是否正确
./nginx -v 检查nginx版本
./nginx -V 大写V 展示nginx版本具体信息
./nginx -? 帮助信息

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值