nginx基本配置

本文详细介绍了如何配置Nginx作为反向代理服务器,实现负载均衡,包括worker_processes、error_log、worker_connections等参数设置,以及HTTPS配置,涉及SSL证书、证书密钥、SSL Cipher等。
摘要由CSDN通过智能技术生成

##个人学习记录,如有问题还望指出。。
#user  nobody;
#工作进程:数目。根据硬件调整,通常等于CPU数量或者2倍于CPU。
worker_processes  1;

#错误日志:存放路径。
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid(进程标识符):存放路径
#pid        logs/nginx.pid;


events {
    #使用epoll的I/O 模型。linux建议epoll,FreeBSD建议采用kqueue,window下不指定。
    use epoll;

    #每个工作进程的最大连接数量。根据硬件调整,和前面工作进程配合起来用,尽量大,但是别把cpu跑到100%就行。
    #每个进程允许的最多连接数,理论上每台nginx服务器的最大连接数为。worker_processes*worker_connections
    worker_connections  64000;
}


##设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
    #设定mime类型,类型由mime.type文件定义
    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指令指定 nginx 是否调用sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为on。
    #如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度,降低系统uptime。
    sendfile        on;
    #此选项允许或禁止使用socke的TCP_CORK的选项,此选项仅在使用sendfile的时候使用
    #tcp_nopush     on;

    #keepalive_timeout  0;keepalive超时时间。
    keepalive_timeout  65;

    #gzip  on;

    #所代理的服务器  backserver对应server中的location 中个proxy_pass...
    upstream backserver { 
         server 10.70.1.16:8088 weight=1 max_fails=2 fail_timeout=30s;  #代理地址
         server 10.70.1.4:8088 weight=1 max_fails=2 fail_timeout=30s;     #代理地址
    } 


            ###配置 http  #######
            
    #客户端通过域名访问服务器时会将域名与被解析的ip一同放在请求中。当请求到了nginx中时。nginx会先去匹配ip,如果listen中没有找到对应的ip,
    #就会通过域名进行匹配,匹配成功以后,再匹配端口。当这三步完成,就会找到对应的server的location对应的资源。
    #没有匹配到且没有指定默认的server_name,将使用第一个server
            
    server {
    
        #配置监听端口 当listen中包含ip时,server_name就失去了意义。可以不用配置。 如 listen ip:端口;
        listen       80;
        #配置访问域名(对外服务地址,即访问host,需匹等于xxxx,也可以使用正则等;如 http://www.xxxx.com/login.jsp, www.xxxx.com需匹配xxxx)
        server_name  xxxx;

        #charset koi8-r;

        #配置访问日志路径,默认 /xx/nginx/log中
        #access_log  logs/host.access.log  main;
        #配置错误日志路径,默认 /xx/nginx/log中
        #error_log 
        

        #对匹配XXXX的地址做负载均衡
        #location 语法
        #(1) location =/url 精确匹配,只有完全匹配url才能生效
        #(2) location ~ pattern 区分大小写的正则表达式匹配
        #(3) location ~* pattern 不区分大小写的正则表达式匹配
        #(4) location ^~ /url 对URL进行前缀匹配,并且在正则表达式之前(注意,这不是一个正则表达式匹配,它的目的是优先于正则表达式匹配)
        #(5) location /url 前缀匹配(在正则匹配之后匹配)
        #(6) location /  匹配所有
        location XXXX { #可以根据需要配置多个location
            
            #root   html;
            #index  index.html index.htm;
            
            #proxy_pass斜杠注意事项:
                #(1) proxy_pass http://xxxxx; http://ip:port/html/test.jsp,被nginx代理后,路径http://proxy_pass/html/test.jsp
                #(2) proxy_pass http://xxxxx/; http://ip:port/html/test.jsp,被nginx代理后,路径http://proxy_pass/test.jsp
            proxy_pass http://backserver/XXXX;
        }
        
        location YYYY {
            
            #root   html;
            #index  index.html index.htm;
            proxy_pass http://backserver/YYYY;
        }

        ##指定404页面  页面内容放置/opt/nginx-1.18.0/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;
        #}
    }


    # 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 start  ######################
    #
    server {
        listen       8071 ssl;
        server_name  diancan.uibe.edu.cn;

        ssl_certificate      /usr/local/nginx/conf/cert/4538865_diancan.uibe.edu.cn.pem;  ##ssl证书路径
        ssl_certificate_key   /usr/local/nginx/conf/cert/4538865_diancan.uibe.edu.cn.key; ##请求认证 key 的路径

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

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

        location diancan.uibe.edu.cn:8071/orderingui/ {
            #root   html;
            #index  index.html index.htm;
            
            proxy_pass http://diancan.uibe.edu.cn:8083/orderingui/;
        }
    }
    
    ###################### HTTPS server end ######################

}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值