Nginx的配置

本文详细介绍了Nginx的配置文件,包括worker_processes的设置以优化性能,日志管理,错误页面配置,以及如何配置HTTP服务器以实现反向代理和负载均衡。还讲解了开启高效文件传输模式、gzip压缩以及虚拟主机的配置,以确保服务器的稳定运行和高效响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在nginx安装路径/conf下 查看nginx的配置文件

cat nginx.conf

下面是对nginx配置文件添加了一些注释

#配置worker进程的运行用户,nobody也是一个linux用户,一般用于启动程序,没有密码
#user  nobody;

#配置工作进程数目,根据硬件调整,通常等于cpu数量或者cpu数量的两倍,我们假如8个cpu,取个中间值6就行
worker_processes  1;

#配置全局错误日志及类型 [debug info notice warn error crit],默认是error
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#配置nginx进程pid文件
#pid        logs/nginx.pid;

#配置工作模式和连接数
events {
    #配置每个worker进程连接数上线(取值上线65535),nginx支持的总连接数就等于worker_processes * worker_connections
    worker_connections  1024; 
}

#配置http服务器,利用它的反向代理功能提供负载均衡支持
http {
    #配置nginx支持哪些多媒体类型,可以在conf/mime.types查看支持哪些多媒体类型
    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日志及存放路径,并使用上面定义的main日志格式,如果打开,则每次访问nginx时会有此日志产生
    #access_log  logs/access.log  main;

    #开启高效文件传输模式
    sendfile        on;
    #防止网络阻塞
    #tcp_nopush     on;

    #长连接超时时间 单位是秒
    #keepalive_timeout  0;
    keepalive_timeout  65;
    
    #开启gzip压缩输出
    #gzip  on;

    #配置虚拟主机 一个http服务器中可以有多个server
    server {
        #配置监听端口
        listen       80;
        #配置服务名 多个server的端口和服务名不能完全一样
        server_name  localhost;

        #配置字符集 默认是utf-8
        #charset koi8-r;

        #配置本虚机主机的访问日志 只有访问到本server才会输出日志
        #access_log  logs/host.access.log  main;

        #请求拦截 默认的匹配斜杠/的请求,当访问路径中有斜杠/时,会被该location匹配到并进行处理
        location / {
            #root(本地磁盘根路径)是配置服务器的默认网站根目录位置,默认是nginx安装主目录下的html目录
            root   html;
            #配置首页文件的名称
            index  index.html index.htm;
        }

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

        # redirect server error pages to the static page /50x.html
        #配置50x错误页面
        #error_page   500 502 503 504  /50x.html;
        
        #精准匹配,访问根路径的50x.html会转发到root的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;
    #    }
    #}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值