Nginx3 基础配置

Nginx配置

Nginx原配置文件


#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; # 引入http mime类型
    default_type  application/octet-stream; # 如果mime类型没匹配上,默认使用二进制流的方式传输。
    #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; # 使用linux的 sendfile(socket, file, len) 高效网络传输,也就是数据0拷贝
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

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

}

sendfile on

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ou3mmQ3T-1667977744779)(https://dinosaurimgs.oss-cn-hangzhou.aliyuncs.com/csdnblog/202211041640825.png)]

sendfile off

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3uqAIQlR-1667977744780)(https://dinosaurimgs.oss-cn-hangzhou.aliyuncs.com/csdnblog/202211041641444.png)]

虚拟机配置

# 虚拟机配置
server {
        listen       80; # 监听的端口号
        server_name  localhost; # 配置域名或者主机,例如www.dinsoaurhou.cn,可以配置多个域名
        # uri匹配
        location / {  # 匹配路径
            root   html; # 文件根目录,可以进行修改
            index  index.html index.htm; # 默认页面名称
        }
        error_page   500 502 503 504  /50x.html; # 报错编码对应页面
        location = /50x.html {
            root   html;
        }
    }

域名解析

在阿里云或者腾讯云购买一个域名,因为在弄自己的Halo博客的时候,买过一个域名dinosaurhou.cn,然后可以使用域名DNS解析,让域名绑定在我的内网ip地址上(192.168.45.128),然后等待解析可以发现我们就可以访问Nginx的主页

Nginx配置使用场景

修改配置文件之后可以重新加载Nginx

systemctl reload nginx

Nginx反向代理

在虚拟机配置中可以修改server下的location / {}添加属性proxy_pass至其他主机地址,反向代理至其他服务器。

Nginx负载均衡

upstream httpds {
    server 192.168.45.129:80 weight=8 down; # 可以填写多个地址进行负载均衡
    server 192.168.45.130:80 weight=2; # 负载均衡策略:weight
    server 192.168.45.131:80 backup;
}

server {
    listen       80;
    server_name  localhost;
    location / {
        proxy_pass http://httpds;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}
负载均衡策略weight、down、backup

weight:可以为不同的代理服务器设置权重

down:可以让其中的代理服务器不使用

backup:备用机,正常使用情况下不使用此服务器

ip_hash:根据客户端的ip地址转发同一台服务器,而可以保持会话

least_conn:最少连接访问

url_hash:根据用户访问的url定向转发请求

fair:根据后端服务器响应时间转发请求

Nginx动静分离

server {
    listen       80;
    server_name  localhost;
    location / {
        proxy_pass http://192.168.45.129:8080; # 反向代理
    }
    location /css {
        root html;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}
正则表达式动静分离
server {
    listen       80;
    server_name  localhost;
    location / {
        proxy_pass http://192.168.45.129:8080; # 反向代理
    }
    location ~*/(css|img|js) {
        root html;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}
URL rewrite
server {
    listen       80;
    server_name  localhost;
    location / {
        rewirte ^/([0-9]+).html$ /index.jsp?pageNum=$1 break; # 若匹配上之后不进行之后匹配
        proxy_pass http://192.168.45.129:8080; # 反向代理
    }
    location ~*/(css|img|js) {
        root html;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}

rewrite是URL重写的关键指令,根据regex(正则表达式)部分内容,重定向到replacement,结尼是flag标记。

rewrite    <regex>   <replacement>  [flag];
关键字				正则				替代内容     flagt标记

正则:per1森容正则表达式语句进行规则匹配
替代内容:将正则匹配的内容替换成replacement

flag标记说明:
last  #本条规则匹配完成后,继续向下匹配新的1ocation URI规则
break #本条规则匹配完成即终止,不再匹配后面的任何规则
redirect #返回302临重定向,游览器地址会显示跳转后的URL地址
permanent #返回301永久重定向,测览器地址栏会显示跳转后的URL地址

Centos 防火墙配置

说明:开启或者关闭指定端口后,需要重启防火墙才能生效

systemctl status firewalld # 防火墙状态
systemctl start firewalld # 防火墙开启
systemctl restart firewalld # 防火墙开启
systemctl stop firewalld # 关闭防火墙
firewall-cmd --permanent --zone=public --add-port=27017/tcp --permanent # 开启指定端口
firewall-cmd --zone=public --remove-port=27017/tcp --permanent # 关闭指定端口
firewall-cmd --permanent --zone=public --add-port=27017-30000/tcp --permanent # 开启指定范围端口
firewall-cmd --permanent --zone=public --add-port=1-65535/tcp --permanent # 开始所有端口
firewall-cmd --zone=public --remove-port=27017-30000/tcp --permanent # 关闭指定范围端口
firewall-cmd --permanent --query-port=27017/tcp # 查看端口是否开放
firewall-cmd --reload # 重启防火墙
firewall-cmd --list-ports # 查看已经开放的端口
iptables -L -n # 查看规则,这个命令是和iptables的相同的
man firewall-cmd  # 查看帮助

参数说明

--zone # 作用域
--add-port=80/tcp # 添加端口,格式为:端口/通讯协议
--remove-port=80/tcp # 移除端口,格式为:端口/通讯协议
--permanent # 永久生效,没有此参数重启后失效

添加富规则允许192.168.45.128访问8080端口

firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.45.128" port protocol="tcp" port="8080" accept"

移除富规则

firewall-cmd --permanent --remove-rich-rule="rule family="ipv4" source address="192.168.45.128" port protocol="tcp" port="8080" accept"

Nginx防盗链

server {
    listen       80;
    server_name  localhost;
    location / {
        rewirte ^/([0-9]+).html$ /index.jsp?pageNum=$1 break; # 若匹配上之后不进行之后匹配
        proxy_pass http://192.168.45.129:8080; # 反向代理
    }
    location ~*/(css|img|js) {
        valid_referers none 192.168.45.128;
        if ($invalid_referer) {
            return 403;
        }
        root html;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}
  • none:检测Referer头域不存在的情况
  • blocked:检测Referer头域的值被防火墙或者代理服务器删除或伪装的情况。这种情况该头域的值以http://https://开头
  • server_names:设置一个或多个URL,检测Referer头域的值是否是这些URL中的某一个
跳转错误页面
server {
    listen       80;
    server_name  localhost;
    location / {
        rewirte ^/([0-9]+).html$ /index.jsp?pageNum=$1 break; # 若匹配上之后不进行之后匹配
        proxy_pass http://192.168.45.129:8080; # 反向代理
    }
    location ~*/(css|img|js) {
        valid_referers none 192.168.45.128;
        if ($invalid_referer) {
            # return 403;
            rewirte ^/    /img/x.png break; # 返回防盗图片
        }
        root html;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
    error_page   401  /401.html; # 错误请求
    location = /401.html {
        root html;
    }
}

结语

经过两周的学习,Nginx算是基本了解了,至少Halo文档中相关的Nginx配置是基本能看懂一点了,会自己配置一点了。了解了什么是负载均衡,什么是反向代理,什么是动静分离,什么是防盗链,还了解了一些Centos的相关操作,以及防火墙的一些命令等。但是我觉得让我最受益的是对计算机网络的一些知识更加了解了,至少对其有一个更加清晰的认识,而不只是局限于书本。

但是我个人感觉自己的学习效率还是不够高的,希望我自己可以继续加把劲!🛩

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
nginx基础配置可以在其配置文件nginx.conf找到。配置文件的路径通常是/nginx/conf/nginx.conf。 在nginx.conf文件,可以设置许多选项来配置nginx的行为。其一些基本的配置选项包括: - http块:在配置文件,http块定义了全局的http配置,包括一些常用的配置项,如mime.types文件的引入、代理配置文件的引入、fastcgi配置文件的引入以及默认的索引文件等。 - server块:在http块,可以有多个server块,每个server块定义了一个虚拟主机的配置信息。可以在每个server块指定域名或IP地址,以及监听的端口号等。 - location块:在server块,可以有多个location块,每个location块定义了一组匹配规则和对应的处理方式。可以通过location块来指定请求的URL匹配规则,并根据规则配置相应的处理方式,如代理请求到其他服务器、处理静态文件等。 此外,nginx还支持在配置文件引入其他文件,以便更好地组织和管理配置。例如,可以在nginx.conf使用include指令来引入其他配置文件,如mime.types、proxy.conf和fastcgi.conf。 总结起来,nginx基础配置可以在nginx.conf文件找到,其包括http块、server块和location块等配置项。可以使用include指令来引入其他配置文件。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Nginx的基本配置](https://blog.csdn.net/weixin_46007090/article/details/120105907)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Nginx基础配置](https://blog.csdn.net/weixin_41968982/article/details/123687834)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值