Nginx 配置

Nginx 配置文件

# 指定 nginx 用户和组(格式:用户 组。Windows 系统不指定)
#user  nobody;

# 指定工作进程数(一般设置成 CPU 核心数或是 CPU 核心数的两倍)
worker_processes  1;

# 指定错误日志为 logs/ 目录下的 error.log 文件
#error_log  logs/error.log;
# 指定错误日志,并指定写入级别为 notice
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
error_log  logs/error.log  error;

# 指定进程可以打开的最大描述符数目
# 这个指令是指一个 nginx 进程打开的最多文件描述符数目,理论值应该是 最多打开文件数(ulimit -n)与 nginx 进程数相除
# 但由于 nginx 分配请求并均匀,所以该值最好与 ulimit -n 保持一致
# 现在 linux 2.6 内核下开启文件打开数为 65535,所以 worker_rlimit_nofile 的值应该为 65535
# 因为 nginx 调度分配请求到工作进程并均衡,所以假设该值为 10240,当总并发量达到 3-4 万时,有些工作进程可能超过 10240 这个数值,返回 502 错误
worker_rlimit_nofile 65535;

# 指定 pid 文件(存放主进程的 pid 号)
#pid        logs/nginx.pid;


# nginx 连接配置模块
events {
    # 指定每个工作进程的最大连接数,这里为 1024
    worker_connections  1024;
}

# http 配置模块
http {
    # 通过 include 加载 mime.types 文件,里面的 types {} 模块将文件扩展名映射到响应的 MIME 类型
    include       mime.types;
    # 定义响应的默认 MIME 类型
    default_type  application/octet-stream;

    # 指定写入 main 的内容格式
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    # 指定访问日志和写入格式为 main
    #access_log  logs/access.log  main;

    # 启用或禁用 sendfile
    sendfile        on;
    # 启用或禁用套接字选项(仅在 sendfile 启用时生效)
    #tcp_nopush     on;

    # 禁用保持活动的客户端连接
    #keepalive_timeout  0;
    # 指定超时时间,这里为 65 秒超时
    keepalive_timeout  65;

    # 启用或禁用 gzip
    #gzip  on;

    # 虚拟主机配置模块(可配置多个)
	server {
            
	        listen 8282;
	        server_name localhost;
	      		location /{
                        more_clear_headers X-Frame-Options;
			proxy_pass	http://www.dg121.com/;
			proxy_set_header Host $http_host; 
			proxy_set_header Upgrade $http_upgrade;
			proxy_set_header Connection "upgrade";
		}
	}


    server {
        # 监听 80 端口
        listen       80;
        # 监听 localhost 域名,多个域名之间用英文逗号分隔
        server_name  localhost;

        #  将指定的 charset 添加到 Content-Type 响应头字段,如果此 charset 与 source_charset 指令中指定的 charset 不同,则进行转换
        #charset koi8-r;

        # 指定该虚拟主机的访问日志
        #access_log  logs/host.access.log  main;

        # 将特定文件或目录重新定位,如 php 文件、image 目录等
        location / {
            # 指定请求根目录
            root   html;
            # 指定索引,按顺序匹配
            index  index.html index.htm;
        }

        # 指定显示 404 错误页面的路径
        #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
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        # 正则表达式匹配 php 文件
        #location ~ \.php$ {
             # 指定代理服务器协议和地址,以及映射位置可选 URI。协议可以指定为 http 或 https。该地址可以指定为一个域名或 IP,以及一个可选端口号
        #    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 服务器的地址。该地址可以指定为一个域名或 IP,以及一个端口号
        #    fastcgi_pass   127.0.0.1:9000;
             # 设置以斜杠结尾 URI 后面追加的文件名
        #    fastcgi_index  index.php;
             # 设置传递给 FastCGI 服务器的参数
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
             # 加载 conf/fastcgi_params 文件
        #    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
    #
    # ssl 配置。启用 ssl 模块需要在编译 nginx 时加上 --with-http_ssl_module 参数
    #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;
    #    }
    #}
}

Nginx 配置文件示例(Windows)

worker_processes 8;

error_log  logs/error.log  error;

worker_rlimit_nofile 3192;

events {
    worker_connections 128;
}

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

    server_names_hash_bucket_size 128;
    client_header_buffer_size 512k;
    large_client_header_buffers 4 512k;
    client_max_body_size 200m;

    client_header_timeout 30;
    client_body_timeout 30;
    send_timeout 30;
    
    sendfile on;
    #tcp_nopush     on;

    keepalive_timeout 60;
  
    open_file_cache max=204800 inactive=60s;
    open_file_cache_min_uses 1;
    open_file_cache_valid 60s;

    tcp_nodelay on;

    gzip on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_types text/plain text/xml text/css application/x-javascript application/xml application/xml+rss text/javascript application/atom+xml image/jpg image/bmp image/png image/gif;
    gzip_vary on;
    #gzip_disable “MSIE [1-6]\.”;

    # cache start #
    proxy_connect_timeout 60;
    proxy_read_timeout 60;
    proxy_send_timeout 60;
    #proxy_buffer_size 16k;
    #proxy_buffers 4 64k;
    #proxy_busy_buffers_size 128k;
    #proxy_temp_file_write_size 128k;
    #proxy_temp_path temp/proxy_temp;
    #proxy_cache_path temp/proxy_temp/cache_temp levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=10g;
    # cache end #

	# 隐藏 nginx 版本信息
	server_tokens off;
	
	upstream 8088 {
		# ip_hash 分流,实现负载均衡(如果网关做了负载,这里不需要配置 ip_hash,也不需要配置多个 server,每个 server 启动一个 nginx 即可通过网关实现负载)
		ip_hash;
		server 127.0.0.1:8088  max_fails=2 fail_timeout=30s;
		server 192.168.1.100:8088  max_fails=2 fail_timeout=30s;
	}
	
	upstream 8089 {
		ip_hash;
		server 127.0.0.1:8089  max_fails=2 fail_timeout=30s;
		server 192.168.1.100:8089  max_fails=2 fail_timeout=30s;
	}
	
	server {
        listen       80;
        server_name  127.0.0.1,192.168.1.100,www.test.com;

		location ^~/mongodb_demo/{
			proxy_pass		http://8088/mongodb_demo/;
			proxy_set_header Host $http_host; 
			proxy_set_header Upgrade $http_upgrade;
			proxy_set_header Connection "upgrade";
			proxy_set_header x-forwarded-for $remote_addr;
			
			# 配置安全过滤规则 #
			add_header X-Frame-Options "SAMEORIGIN";
			add_header X-Content-Options "nosniff";
			add_header X-Content-Type-Options "nosniff";
			add_header X-XSS-Protection "1;mode=block";

			error_log logs/info.log debug;
            header_filter_by_lua_block { 
                    local request_method = ngx.var.request_method;
            local data="";
            local headers = ngx.req.get_headers()
            local referer=headers["Referer"];
            if referer and not(string.find(referer,"localhost")) and not(string.find(referer,"127.0.0.1")) then
            	ngx.exit(500);
            end;
            if request_method == "GET" then
            	data = ngx.var.query_string ;
            elseif request_method == "POST" then
            	data = ngx.req.get_body_data();
            end;
            if not(string.find(request_method, "GET") or string.find(request_method, "POST") or string.find(request_method, "HEAD") or string.find(request_method, "OPTIONS"))  then
            	ngx.exit(501);                
            end;
            local type = ngx.header["Content-Type"];
            if data and type and  not(string.find(type,"image")) then 
            	if string.find(data, "'") or string.find(data, ";") or string.find(data, "%%3B") or string.find(data, " and") or string.find(data, "%%20and") or string.find(data, " or")or string.find(data, "%%20or") then
                	ngx.exit(502);
                end;
				if string.find(data,"*") and not(string.find(data,"*&")) and not(string.find(data,"outFields=*")) then
                	ngx.exit(503);
                end;
            end
        
        	more_set_headers "Access-Control-Allow-Credentials:true";
        	more_set_headers "Access-Control-Allow-Origin:$http_origin";
			# 配置安全过滤规则 #
		}
		
		location ^~/redis_demo/{
			proxy_pass		http://8089/redis_demo/;
			proxy_set_header Host $http_host; 
			proxy_set_header Upgrade $http_upgrade;
			proxy_set_header Connection "upgrade";
			proxy_set_header x-forwarded-for $remote_addr;

			# 配置安全过滤规则 #
			add_header X-Frame-Options "SAMEORIGIN";
			add_header X-Content-Options "nosniff";
			add_header X-Content-Type-Options "nosniff";
			add_header X-XSS-Protection "1;mode=block";
			# 配置安全过滤规则 #
		}
	}

	# ssl 配置(如果网关配置了 ssl,这里不需要进行配置)
    server {
    	# ssl 访问端口号为 443
    	listen 443 ssl; 
 		# 绑定证书域名
    	server_name www.test.com; 
 		# 证书文件名称(证书文件需要拷贝到配置文件同目录下)
    	ssl_certificate 1_www.test.com_bundle.crt; 
 		# 私钥文件名称(私钥文件需要拷贝到配置文件同目录下)
    	ssl_certificate_key 2_www.test.com.key; 
    	ssl_session_timeout 5m;
    	ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 
 		# 配置加密套件,写法遵循 openssl 标准
		ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; 
		ssl_prefer_server_ciphers on;
		location / {
			proxy_pass  http://127.0.0.1:80;
            	proxy_redirect     off;
                proxy_set_header   Host             $host;
                proxy_set_header   X-Real-IP        $remote_addr;
                proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    	}
	}	
}

Nginx 配置文件示例(macOS)

macOS 通过 nginx 映射项目与 Windows 稍有不同,当时这个问题困惑了许久,这里记录一下。macOS 需要配置项目所在根目录,如

root /Users/root/code/test;
worker_processes 8;

error_log  logs/error.log  error;

worker_rlimit_nofile 3192;

events {
    worker_connections 128;
}

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

    server_names_hash_bucket_size 128;
    client_header_buffer_size 512k;
    large_client_header_buffers 4 512k;
    client_max_body_size 200m;

    client_header_timeout 30;
    client_body_timeout 30;
    send_timeout 30;
    
    sendfile on;
    #tcp_nopush     on;

    keepalive_timeout 60;
  
    open_file_cache max=204800 inactive=60s;
    open_file_cache_min_uses 1;
    open_file_cache_valid 60s;

    tcp_nodelay on;

    gzip on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_types text/plain text/xml text/css application/x-javascript application/xml application/xml+rss text/javascript application/atom+xml image/jpg image/bmp image/png image/gif;
    gzip_vary on;
    #gzip_disable “MSIE [1-6]\.”;

    # cache start #
    proxy_connect_timeout 60;
    proxy_read_timeout 60;
    proxy_send_timeout 60;
    #proxy_buffer_size 16k;
    #proxy_buffers 4 64k;
    #proxy_busy_buffers_size 128k;
    #proxy_temp_file_write_size 128k;
    #proxy_temp_path temp/proxy_temp;
    #proxy_cache_path temp/proxy_temp/cache_temp levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=10g;
    # cache end #

	# 隐藏 nginx 版本信息
	server_tokens off;
	
	upstream 8088 {
		# ip_hash 分流,实现负载均衡(如果网关做了负载,这里不需要配置 ip_hash,也不需要配置多个 server,每个 server 启动一个 nginx 即可通过网关实现负载)
		ip_hash;
		server 127.0.0.1:8088  max_fails=2 fail_timeout=30s;
		server 192.168.1.100:8088  max_fails=2 fail_timeout=30s;
	}
	
	upstream 8089 {
		ip_hash;
		server 127.0.0.1:8089  max_fails=2 fail_timeout=30s;
		server 192.168.1.100:8089  max_fails=2 fail_timeout=30s;
	}
	
	server {
        listen       80;
        server_name  127.0.0.1,192.168.1.100,www.test.com;
        # 如果 mongodb_demo 与 redis_demo 两个项目均位于 /Users/root/code/test 目录下,则可以在 server 标签中统一配置
        root /Users/root/code/test;

		location ^~/mongodb_demo/{
			#  如果 mongodb_demo 与 redis_demo 两个项目所在目录不同,则需要在 location 标签中分别配置
			root /Users/root/code/mongodb;
			
			proxy_pass		http://8088/mongodb_demo/;
			proxy_set_header Host $http_host; 
			proxy_set_header Upgrade $http_upgrade;
			proxy_set_header Connection "upgrade";
			proxy_set_header x-forwarded-for $remote_addr;
			
			# 配置安全过滤规则 #
			add_header X-Frame-Options "SAMEORIGIN";
			add_header X-Content-Options "nosniff";
			add_header X-Content-Type-Options "nosniff";
			add_header X-XSS-Protection "1;mode=block";

			error_log logs/info.log debug;
            header_filter_by_lua_block { 
                    local request_method = ngx.var.request_method;
            local data="";
            local headers = ngx.req.get_headers()
            local referer=headers["Referer"];
            if referer and not(string.find(referer,"localhost")) and not(string.find(referer,"127.0.0.1")) then
            	ngx.exit(500);
            end;
            if request_method == "GET" then
            	data = ngx.var.query_string ;
            elseif request_method == "POST" then
            	data = ngx.req.get_body_data();
            end;
            if not(string.find(request_method, "GET") or string.find(request_method, "POST") or string.find(request_method, "HEAD") or string.find(request_method, "OPTIONS"))  then
            	ngx.exit(501);                
            end;
            local type = ngx.header["Content-Type"];
            if data and type and  not(string.find(type,"image")) then 
            	if string.find(data, "'") or string.find(data, ";") or string.find(data, "%%3B") or string.find(data, " and") or string.find(data, "%%20and") or string.find(data, " or")or string.find(data, "%%20or") then
                	ngx.exit(502);
                end;
				if string.find(data,"*") and not(string.find(data,"*&")) and not(string.find(data,"outFields=*")) then
                	ngx.exit(503);
                end;
            end
        
        	more_set_headers "Access-Control-Allow-Credentials:true";
        	more_set_headers "Access-Control-Allow-Origin:$http_origin";
			# 配置安全过滤规则 #
		}
		
		location ^~/redis_demo/{
			#  如果 mongodb_demo 与 redis_demo 两个项目所在目录不同,则需要在 location 标签中分别配置
			root /Users/root/code/redis;
			
			proxy_pass		http://8089/redis_demo/;
			proxy_set_header Host $http_host; 
			proxy_set_header Upgrade $http_upgrade;
			proxy_set_header Connection "upgrade";
			proxy_set_header x-forwarded-for $remote_addr;

			# 配置安全过滤规则 #
			add_header X-Frame-Options "SAMEORIGIN";
			add_header X-Content-Options "nosniff";
			add_header X-Content-Type-Options "nosniff";
			add_header X-XSS-Protection "1;mode=block";
			# 配置安全过滤规则 #
		}
	}

	# ssl 配置(如果网关配置了 ssl,这里不需要进行配置)
    server {
    	# ssl 访问端口号为 443
    	listen 443 ssl; 
 		# 绑定证书域名
    	server_name www.test.com; 
 		# 证书文件名称(证书文件需要拷贝到配置文件同目录下)
    	ssl_certificate 1_www.test.com_bundle.crt; 
 		# 私钥文件名称(私钥文件需要拷贝到配置文件同目录下)
    	ssl_certificate_key 2_www.test.com.key; 
    	ssl_session_timeout 5m;
    	ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 
 		# 配置加密套件,写法遵循 openssl 标准
		ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; 
		ssl_prefer_server_ciphers on;
		location / {
			proxy_pass  http://127.0.0.1:80;
            	proxy_redirect     off;
                proxy_set_header   Host             $host;
                proxy_set_header   X-Real-IP        $remote_addr;
                proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    	}
	}	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值