nginx之nginx.conf基本配置1

时间:2022-10-14 12:18:53
1.
#user  nobody;
worker_processes  2;
events {
	worker_connections  1024;
}

http {
include mime.types;
default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;


upstream localhost{ 
#这里指定多个源服务器,ip:端口,80端口的话可写可不写 
server 127.0.0.1:8080; 
}

server {
listen 80;
server_name localhost;

location / {
#启动代理
proxy_pass http://localhost;
}
}
}


2.静态文件配置

#user  nobody;
worker_processes  4;#推荐worker数为cpu核数,避免cpu不必要的上下文切换
events {
    #表示每个worker进程所能建立连接的最大值
    #一个nginx最大的连接数max=worker_connections*worker_processes;
    #对于http请求本地资源最大并发数量为max
    #如果http作为反向代理,最大并发数为max/2。因为每个并发会建立与客户端的连接和与后端服务的连接,会占用两个连接。
    worker_connections  1024;
}
http {
    include       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  logs/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;
        access_log  logs/host.access.log  main;
        #location / {
        #    root   html;
        #    index  index.html index.htm;
        # }
	location / {
            root   E:/BaiduNetdiskDownload/boostrap3; #静态文件路径
            index  index.html index.htm;  #访问首页,在静态文件路径根目录
         }
        # serve static files(css|js|image..)
        #
	#静态文件交给nginx处理
	location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|css|js|ttf|woff|woff2)$
	{
        #location ~ ^/(img|javascript|js|css|flash|media|static)/  {
	# img|javascript|js|css|flash|media|static 对应着静态文件路径下的文件夹
          root       E:/BaiduNetdiskDownload/boostrap3;
          access_log  off;
          expires     30d;
        }
        #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;
        }
    }
}


例如:访问img下的1.png图片;http://127.0.0.1/img/1.png

nginx之nginx.conf基本配置1