【Nginx】加载其他conf文件

(打开服务:start nginx)

【第一步】在nginx.con文件中配置:

导入外部服务器配置文件存放地址

include /confs/*.conf; #confs是自己创建的文件夹


#user  nobody;
worker_processes  1;

#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 {
    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;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8888;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
			proxy_pass   http://localhost:5000;
        }
		location /a/ {
			proxy_pass   http://localhost:8004/;
        }
        #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:5180;
        #}

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

   include confs/*.conf;#这里是自己创建的confs文件夹
}

介绍:nginx.conf配置文件分为三部分

【一】全局块:

从配置文件开始到 events 块之间的内容,主要会设置一些影响nginx 服务器整体运行的配置指令,主要包括配置运行 Nginx 服务器的用户(组)、允许生成的 worker process 数,进程 PID 存放路径、日志存放路径和类型以及配置文件的引入等。

worker_processes  1;

  这是 Nginx 服务器并发处理服务的关键配置,worker_processes 值越大,可以支持的并发处理量也越多,但是会受到硬件、软件等设备的制约。

【二】events 块

比如上面的配置:

events {
    worker_connections  1024;
}
 events 块涉及的指令主要影响 Nginx 服务器与用户的网络连接,常用的设置包括是否开启对多 work process 下的网络连接进行序列化,是否允许同时接收多个网络连接,选取哪种事件驱动模型来处理连接请求,每个 word process 可以同时支持的最大连接数等。

  上述例子就表示每个 work process 支持的最大连接数为 1024.

  这部分的配置对 Nginx 的性能影响较大,在实际中应该灵活配置。

【三】http块

这算是 Nginx 服务器配置中最频繁的部分,代理、缓存和日志定义等绝大多数功能和第三方模块的配置都在这里。

  需要注意的是:http 块也可以包括 http全局块server 块

①、http 全局块

  http全局块配置的指令包括文件引入、MIME-TYPE 定义、日志自定义、连接超时时间、单链接请求数上限等。

②、server 块

  这块和虚拟主机有密切关系,虚拟主机从用户角度看,和一台独立的硬件主机是完全一样的,该技术的产生是为了节省互联网服务器硬件成本。后面会详细介绍虚拟主机的概念。

  每个 http 块可以包括多个 server 块,而每个 server 块就相当于一个虚拟主机。

  而每个 server 块也分为全局 server 块,以及可以同时包含多个 locaton 块。

  1、全局 server 块

  最常见的配置是本虚拟机主机的监听配置和本虚拟主机的名称或IP配置。

  2、location 块

  一个 server 块可以配置多个 location 块。

  这块的主要作用是基于 Nginx  服务器接收到的请求字符串(例如 server_name/uri-string),对虚拟主机名称(也可以是IP别名)之外的字符串(例如 前面的 /uri-string)进行匹配,对特定的请求进行处理。地址定向、数据缓存和应答控制等功能,还有许多第三方模块的配置也在这里进行。

【第二步】在confs文件下创建自己的conf文件,配置

server {
        listen       6006;
        server_name  localhost;
        #charset koi8-r;
		# 缓存
		proxy_buffer_size 128k;
		proxy_buffers   32 128k;
		
		proxy_busy_buffers_size 128k;
		
		proxy_temp_file_write_size 10m;
		
		
		access_log       logs/ceshi.access.log;
	    error_log        logs/ceshi.error.log;

        location / {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Credentials' 'true';
            root   'D:/publish/ceshifront/dist/';#前端发布的地址
            index  index.html index.html;
			try_files $uri $uri/ /index.html; #加上这一行
        }

		
		location /userdata/ {
			alias D:/publish/ceshi/;#后端发布的地址
		}
        location /default/ {
			proxy_pass http://localhost:5000/;
        error_page 405 =200 http://$host$request_uri;#若出现405应该可以解决的
		}
        location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md){
            return 403;
        }
}

【第三步】重启nginx(nginx -s reload)

打开浏览器输入地址

### 配置和使用 Nginx 的 `lua.conf` 文件 为了使 Lua 脚本能够在 Nginx 中运行,需确保安装了 OpenResty 或者带有 ngx_http_lua_module 模块的 Nginx 版本。配置过程涉及编辑 Nginx 主配置文件或特定站点配置文件来引入 Lua 支持。 #### 创建并编辑 `lua.conf` 创建一个新的配置文件命名为 `lua.conf` 并放置于 Nginxconf.d 目录下: ```bash vi /etc/nginx/conf.d/lua.conf ``` 在该文件内定义一个虚拟主机服务如下所示[^1]: ```nginx server { listen 80; server_name localhost; location /lua { default_type 'text/html'; lua_code_cache off; content_by_lua_file /usr/local/openresty/lua/test.lua; } } ``` 上述配置指定了当访问 `/lua` URI 时会执行位于指定路径下的 Lua 脚本文件 (`test.lua`) 来响应客户端请求。这里关闭了 Lua 编译缓存(`lua_code_cache off`)以便开发调试阶段可以即时看到更改效果;生产环境中建议开启此选项以提高性能。 #### 测试 Lua 脚本 准备一段简单的 Lua 程序作为测试脚本保存至前述配置中提到的位置: ```lua -- test.lua ngx.say("Hello, world!") ``` 这段代码会在浏览器端显示 "Hello, world!" 文字串。 #### 加载新配置 完成以上设置后记得重新加载 Nginx 让改动生效: ```bash nginx -s reload ``` 如果遇到 `[emerg] unknown directive` 错误提示,则可能是由于缺少必要的模块支持或是语法错误引起[^2]。确认已正确编译并启用了 Lua 处理能力,并仔细核对配置项书写无误后再尝试重启服务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值