nginx 学习笔记(二) http、https、utp/tcp 代理配置 nginx.conf

本文深入探讨了Nginx的配置体系,包括安全设置、日志管理、并发连接优化、TCP/UDP代理配置、HTTP请求处理以及静态资源缓存策略等关键组件。旨在帮助开发者实现高效、稳定且安全的Web服务器部署。

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

安全问题,建议用nobody,不要用root.
user root;

worker数和服务器的cpu数相等是最为适宜
worker_processes 1;

error_log path(存放路径) level(日志等级)path表示日志路径,level表示日志等级,
具体如下:[ debug | info | notice | warn | error | crit ]
从左至右,日志详细程度逐级递减,即debug最详细,crit最少,默认为crit。
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;
pid logs/nginx.pid;
events {

#这个值是表示每个worker进程所能建立连接的最大值,所以,一个nginx能建立的最大连接数,

#应该是worker\_connections * worker\_processes。  
#当然,这里说的是最大连接数,对于HTTP请求本地资源来说,能够支持的最大并发数量是worker\_connections * worker\_processes,  
#如果是支持http1.1的浏览器每次访问要占两个连接,  
#所以普通的静态访问最大并发数是: worker\_connections * worker\_processes /2,  
#而如果是HTTP作为反向代理来说,最大并发数量应该是worker\_connections * worker\_processes/4。  
#因为作为反向代理服务器,每个并发会建立与客户端的连接和与后端服务的连接,会占用两个连接。  
worker_connections  1024;

#这个值是表示nginx要支持哪种多路io复用。
#一般的Linux选择epoll, 如果是(*BSD)系列的Linux使用kquene。
#windows版本的nginx不支持多路IO复用,这个值不用配。
use epoll;

# 当一个worker抢占到一个链接时,是否尽可能的让其获得更多的连接,默认是off 。  
multi_accept on;

# 默认是on ,开启nginx的抢占锁机制。  
accept_mutex  on;

}

tcp/udp 代理
stream {

upstream backend {

# 1、轮询(默认)  
# 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。  
# 2、指定权重  
# 指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。  
#3、IP绑定 ip_hash  
# 每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。  
#4、备机方式 backup  
# 正常情况不访问设定为backup的备机,只有当所有非备机全都宕机的情况下,服务才会进备机。  
#5、fair(第三方)  
#按后端服务器的响应时间来分配请求,响应时间短的优先分配。     
#6、url_hash(第三方)  
#按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。  
  #还可以设置请求失败的次数。max_fails  
    #hash $remote_addr consistent;  
    # 代理tcp,都代理到3306端口,但是weight(权重)不一样,代理到252服务的概率为50%,代理到251服务的概率为30%.  
    server 192.168.1.252:3306 weight=5 max\_fails=3 fail\_timeout=30s;  
    server 192.168.1.253:3306 weight=2 max\_fails=3 fail\_timeout=30s;  
}  

#配置tcp  

server {
listen 3306;
proxy_connect_timeout 3s;
proxytimeout 10s; proxypass backend;

}

upstream dnsservers{ server 192.168.47.44:53; } #配置udp server { listen 53 udp; #允许访问的IP allow 192.168.21.216; #除216之外的IP都不允许访问 deny all; #UDP traffic will be proxied to the "dnsservers" upstream group
proxy_pass dns_servers;
}

}

http {

# lua 脚本的配置  
  lua\_package\_path "/usr/local/src/nginx/conf/waf/?.lua";  
  lua\_shared\_dict limit 10m;  
  init\_by\_lua_file /usr/local/src/nginx/conf/waf/init.lua;  
 access\_by\_lua_file /usr/local/src/nginx/conf/waf/waf.lua;

#当web服务器收到静态的资源文件请求时,依据请求文件的后缀名在服务器的MIME配置文件中找到对应的MIME Type,再根据MIME Type设置HTTP Response的Content-Type,然后浏览器根据Content-Type的值处理文件。
include mime.types;

#如果 不能从mime.types找到映射的话,用以下作为默认值
default_type application/octet-stream;

#关闭反向代理缓存
proxy_buffering off;

#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;

#开启从磁盘直接到网络的文件传输,适用于有大文件上传下载的情况,提高IO效率。

sendfile        on;  
#tcp_nopush     on;

#一个请求完成之后还要保持连接多久, 默认为0,表示完成请求后直接关闭连接。

#keepalive_timeout  0;  
keepalive_timeout  65;

#开启或者关闭gzip模块

#gzip  on;  

#设置允许压缩的页面最小字节数,页面字节数从header头中的Content-Length中进行获取。
#gzip_min_lenth 1k;

# gzip压缩比,1 压缩比最小处理速度最快,9 压缩比最大但处理最慢(传输快但比较消耗cpu)  
#gzip\_comp\_level 4;

#匹配MIME类型进行压缩,(无论是否指定)"text/html"类型总是会被压缩的。  
#gzip_types types text/plain text/css application/json  application/x-javascript text/xml 

#动静分离  
#服务器端静态资源缓存,最大缓存到内存中的文件,不活跃期限  
open\_file\_cache max=655350 inactive=20s;     
#活跃期限内最少使用的次数,否则视为不活跃。  
open\_file\_cache\_min\_uses 2;

#验证缓存是否活跃的时间间隔  
open\_file\_cache_valid 30s;

   server {  
    listen       81;  
    server_name  nginx.test1.com;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

#location \[=|~|~*|^~\] /uri/ { … }     
# = 精确匹配  
# ~ 正则匹配,区分大小写  
# ~\* 正则匹配,不区分大小写  
# ^~  关闭正则匹配  
#匹配原则:  
# 1、所有匹配分两个阶段,第一个叫普通匹配,第二个叫正则匹配。  
# 2、普通匹配,首先通过“=”来匹配完全精确的location  
    #   2.1、 如果没有精确匹配到, 那么按照最大前缀匹配的原则,来匹配location  
    #   2.2、 如果匹配到的location有^~,则以此location为匹配最终结果,如果没有那么会把匹配的结果暂存,继续进行正则匹配。  
    # 3、正则匹配,依次从上到下匹配前缀是~或~*的location, 一旦匹配成功一次,则立刻以此location为准,不再向下继续进行正则匹配。  
    # 4、如果正则匹配都不成功,则继续使用之前暂存的普通匹配成功的location.

# 匹配任何查询,因为所有请求都已 / 开头。但是正则表达式规则和长的块规则将被优先和查询匹配。

location / {
#定义服务器的默认网站根目录位置
root html;
#默认访问首页索引文件的名称
index index.html index.htm;

    #反向代理路径  
        proxy_pass http://192.168.2.3/test;

    #反向代理的超时时间  
        proxy\_connect\_timeout 10;

        proxy_redirect default;       

     }

 # auth\_request表示需要调用第三方接口进行授权,后台response.setStatus(200)返回时会继续走proxy\_pass 代理。

    location /test {  
     auth_request /auth;  
     proxy_pass http://192.168.40.26:8080/cloud/wapi/v1.0/hello;  
    }  
       location /auth {  
        proxy_pass http://192.168.40.26:8080/cloud/wapi/v1.0/auth/;  
        #proxy\_pass\_request_body off;  
        #proxy\_set\_header Content-Length "";  
        #proxy\_set\_header X-Original-URI $request_uri;  
    }  


 }  
server {  
    listen       80;  
    server_name  localhost;  
    #charset koi8-r;

    #access_log  logs/host.access.log  main;

      proxy\_set\_header   Host   $host;  
      proxy\_set\_header   Referer $http_referer;  
      proxy\_set\_header   Cookie $http_cookie;  
      proxy\_set\_header   X-Real-IP  $remote_addr;  
      proxy\_set\_header   X-Forwarded-For              $proxy\_add\_x\_forwarded\_for;

  # 访问test2时,会通过lua脚本的access\_by\_lua调用后台方法v1.0/auth方法。

    location /test2 {  
     access\_by\_lua 'local res = ngx.location.capture("/v1.0/auth")  
        if res.status == ngx.HTTP_OK then  
        return  
        end  
        if res.status == ngx.HTTP_FORBIDDEN then  
        ngx.exit(res.status)  
        end  
        ngx.exit(ngx.HTTP\_INTERNAL\_SERVER_ERROR)  
        ';  
     proxy_pass http://192.168.40.119:8888/ipam-server;  

    }

  #调用后台方法v1.0/auth方法 时会被代理到192.168.40.26:8080/v1.0/auth上去。  
    location ~ /v1.0/(.*) {  
          internal;  
         proxy_pass http://192.168.40.26:8080;  
     }

    #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  nginx.test2.com;

  # 通过 openssl 生成对应的证书

   ssl_certificate      /usr/local/src/nginx/ssl/nginx.crt;   
   ssl\_certificate\_key  /usr/local/src/nginx/ssl/nginx.key;

   ssl\_session\_cache    shared:SSL:1m;  
   ssl\_session\_timeout  5m;

   ssl_ciphers  HIGH:!aNULL:!MD5;  
   ssl\_prefer\_server_ciphers  on;

    location = /favicon.ico {  
        log\_not\_found off;  
        access_log off;  
    }

  # https的代理单点登录

    location /cas/login {  
      proxy\_set\_header Host $host:$server_port;    
      proxy\_set\_header   Referer $http_referer;  
      proxy\_set\_header   Cookie $http_cookie;  
      proxy\_set\_header X-Real-IP $remote_addr;    
      proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for;  
      proxy\_set\_header X-FORWARDED-HOST $server_addr;    
      proxy\_set\_header X-FORWARDED-PORT $server_port;

     # 单点登录的服务器的IP  
      proxy_pass https://192.168.40.119:8090/cas/login;  
    }

  # 登录单点登录时所需的css、js、jpg等文件

    location ~*\\.(gif|jpg|jpeg|png|css|js|jsp|htm)$ {          

      # 此处不能配置 https://192.168.40.119:8090/ 否则会报错                                                                                           
         proxy_pass https://192.168.40.119:8090;  
    }  

 }

}

 

<think>好的,我现在需要帮助用户分析他们运行`sudo find / -name "nginx.conf" 2>/dev/null`后得到的多个nginx.conf文件路径。用户之前的问题是关于如何确定正确的Nginx配置文件位置,现在他们提供了查找结果,里面有几个可能的路径,需要我解释这些结果并指导他们确定正确的配置文件。 首先,我要理解用户的需求。他们可能在配置Nginx时遇到问题,或者需要确认哪个配置文件是当前正在使用的。用户之前尝试过用`nginx -V`来查找配置路径,但没有成功,现在通过find命令找到了多个路径,需要帮助筛选。 接下来,我需要分析用户提供的路径。给出的路径有: 1. `/root/nginx/nginx-rtmp-module-1.2.2/test/nginx.conf` 2. `/root/nginx/nginx-1.25.5/conf/nginx.conf` 3. `/usr/local/nginx/conf/nginx.conf` 4. `/www/server/nginx/nginx-rtmp-module/test/nginx.conf` 5. `/www/server/nginx/conf/nginx.conf` 6. `/www/backup/file_history/www/server/nginx/conf/nginx.conf` 首先,我需要排除测试或备份文件。比如路径中包含`test`或`backup`的可能是测试用例或备份文件,不太可能是当前使用的配置。例如,路径1和4中的`test`目录下的nginx.conf,通常是模块的测试配置文件,而不是实际使用的。路径6看起来像是备份文件,因为路径中有`backup/file_history`。 剩下的可能性较大的路径是: - `/root/nginx/nginx-1.25.5/conf/nginx.conf`:这可能是在用户自己编译安装Nginx时生成的配置文件,尤其是如果用户手动下载并编译了Nginx 1.25.5版本,这个路径可能对应源码安装的位置。 - `/usr/local/nginx/conf/nginx.conf`:这也是常见的源码安装的默认路径,通常当用户通过源码编译安装且没有指定其他路径时,配置文件会在这里。 - `/www/server/nginx/conf/nginx.conf`:这个路径看起来像是通过某些第三方脚本或面板(如宝塔面板)安装的Nginx配置路径,很多控制面板会自定义安装目录到`/www/server`下。 接下来,用户需要确定哪个是当前正在使用的配置文件。我需要建议用户使用之前提到的`nginx -t`命令来测试配置,这会显示实际加载的配置文件路径。如果用户运行`nginx -t`,输出中会显示正在使用的配置文件,例如“configuration file /etc/nginx/nginx.conf syntax is ok”,这样就能明确知道正确的路径。 如果用户没有权限运行`nginx -t`,可能需要使用`sudo`。此外,检查Nginx进程的启动参数也是一个方法,通过`ps aux | grep nginx`查看主进程是否带有`-c`参数指定配置文件,例如`-c /www/server/nginx/conf/nginx.conf`,这样可以直接确认。 另外,用户可能安装了多个Nginx实例,比如通过不同方式安装的,这时候需要确认当前运行的Nginx进制文件的位置,使用`which nginx`或者`ps aux | grep nginx`找到进制路径,再根据该路径推断配置文件的默认位置。 最后,需要提醒用户,如果存在多个配置文件,修改错误的文件不会生效,因此确认正确的路径非常重要。建议用户优先检查`/www/server/nginx/conf/nginx.conf`,因为第三方面板常用此路径,同时结合`nginx -t`的输出来验证。</think>根据你提供的 `find` 命令结果,系统中存在 **多个 `nginx.conf` 文件**,这表明可能存在以下情况: --- ### **路径分析** 1. **测试/备份文件(可忽略)** - `/root/nginx/nginx-rtmp-module-1.2.2/test/nginx.conf` - `/www/server/nginx/nginx-rtmp-module/test/nginx.conf` - `/www/backup/file_history/www/server/nginx/conf/nginx.conf` **说明**:这些是 **Nginx RTMP 模块的测试配置文件** 或 **历史备份文件**,与当前运行中的 Nginx 无关。 2. **源码编译安装路径** - `/root/nginx/nginx-1.25.5/conf/nginx.conf` - `/usr/local/nginx/conf/nginx.conf` **说明**:可能是你通过源码编译安装 Nginx 时生成的默认配置文件,但若未实际运行此版本,则非当前生效配置。 3. **重点怀疑路径** - `/www/server/nginx/conf/nginx.conf` **说明**:此路径常见于通过 **第三方脚本(如宝塔面板、LNMP一键包)** 安装的 Nginx,极有可能是当前正在使用的配置文件。 --- ### **如何确认实际生效的配置文件?** #### **方法一:通过 `nginx -t` 验证配置** 运行命令查看 Nginx 实际加载的配置文件路径: ```bash sudo nginx -t ``` 输出示例: ``` nginx: [alert] could not open error log file: open() "/var/log/nginx/error.log" failed (13: Permission denied) nginx: the configuration file **/www/server/nginx/conf/nginx.conf** syntax is ok nginx: configuration file /www/server/nginx/conf/nginx.conf test is successful ``` **第一行**会显示当前配置文件路径(即使权限错误也会输出路径)。 #### **方法:查看 Nginx 进程参数** 运行命令检查进程启动时指定的配置文件: ```bash ps aux | grep nginx | grep master ``` 输出示例: ``` root 1234 0.0 0.1 12345 6789 ? Ss 12:34 0:00 nginx: master process **/www/server/nginx/sbin/nginx -c /www/server/nginx/conf/nginx.conf** ``` 若存在 `-c` 参数,则直接显示配置文件路径。 --- ### **最终结论** 根据路径特征和常见安装方式,**当前生效的配置文件应为 `/www/server/nginx/conf/nginx.conf`**。建议按以下步骤操作: 1. **验证配置语法**: ```bash sudo nginx -t -c /www/server/nginx/conf/nginx.conf ``` 2. **查看配置关联文件**: 配置文件通常包含 `include` 指令加载其他配置(如虚拟主机),例如: ```nginx include /www/server/nginx/conf/vhost/*.conf; ``` 3. **若需修改配置**: 直接编辑 `/www/server/nginx/conf/nginx.conf`,修改后执行 `sudo nginx -s reload` 重载配置--- ### **其他注意事项** - **多版本共存**:若系统中存在多个 Nginx 实例,需通过 `which nginx` 确认当前运行的进制文件路径,再结合 `ps aux` 进一步分析。 - **权限问题**:操作配置文件时可能需要 `sudo` 提权。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值