CentOS&.NET Core初试-3-Nginx的安装和配置

CentOS&.NET Core初试-3-Nginx的安装和配置,第1张

概述Nginx简介 ??Nginx是一个免费的,开源的,高性能的HTTP服务器和反向代理,以及IMAP / POP3代理服务器。 ??Nginx以其高性能,稳定性,丰富的功能集,简单的配置和低资源消耗而闻名。 ??Nginx使用更加可扩展的事件驱动(异步)架构,此体系结构在负载下使用较小但更重要的可预测内存量。即使您不希望同时处理数千个请求,您仍然可以从Nginx的高性能和小内存占用中受益。 ??Ng Nginx简介

??Nginx是一个免费的,开源的,高性能的http服务器和反向代理,以及IMAP / POP3代理服务器。
??Nginx以其高性能,稳定性,丰富的功能集,简单的配置和低资源消耗而闻名。
??Nginx使用更加可扩展的事件驱动(异步)架构,此体系结构在负载下使用较小但更重要的可预测内存量。即使您不希望同时处理数千个请求,您仍然可以从Nginx的高性能和小内存占用中受益。
??Nginx可以向各个方向扩展:从最小的VPS一直到大型服务器集群。

安装Nginx 安装 epel
sudo yum install epel-release
安装 Nginx
sudo yum install Nginx
启动 Nginx

Nginx 不会自己启动,启动命令:

sudo systemctl start Nginx
关闭防火墙

??如果外部浏览器输入该机IP还是访问不了,说明有防火墙正在运行,关闭http 和 httpS的防火墙:

sudo firewall-cmd --permanent --zone=public  --add-service=httpsudo firewall-cmd --permanent --zone=public --add-service=httpssudo firewall-cmd --reload
访问验证

外部浏览器访问成功,说明Nginx安装成功。

开机启动

最后,因为Nginx默认是不主动开启的,为了能够在系统启动就开启Nginx:

sudo systemctl enable Nginx
端口映射配置 查看Nginx.conf
vi /etc/Nginx/Nginx.conf
修改Nginx配置

Nginx.conf文件http配置内容,并且i进去注释掉http配置下server的默认配置内容Esc+:wq保存后退出。

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  /var/log/Nginx/access.log  main;    sendfile            on;    tcp_nopush          on;    tcp_nodelay         on;    keepalive_timeout   65;    types_hash_max_size 2048;    include             /etc/Nginx/mime.types;    default_type        application/octet-stream;    # Load modular configuration files from the /etc/Nginx/conf.d directory.    # See http://Nginx.org/en/docs/ngx_core_module.HTML#include    # for more information.    include /etc/Nginx/conf.d/*.conf;    #【解释】Nginx会加载 /etc/Nginx/conf.d文件夹下所有的conf文件的配置 #   server { #       Listen       80 default_server; #       Listen       [::]:80 default_server; #        server_name  _;  #        root         /usr/share/Nginx/HTML; # #        # Load configuration files for the default server block. #        include /etc/Nginx/default.d/*.conf; # #        location / { #        } # #        error_page 404 /404.HTML; #            location = /40x.HTML { #        } # #        error_page 500 502 503 504 /50x.HTML; #            location = /50x.HTML { #        } #    }
创建新配置

根据Nginx配置文件有提示,在/etc/Nginx/conf.d文件夹下为hellocore项目新建一个netcore.conf文件,文件配置如下

server {    Listen       80;    location / {    proxy_pass http://localhost:5000;    proxy_http_version 1.1;    proxy_set_header Upgrade $http_upgrade;    proxy_set_header Connection keep-alive;    proxy_set_header Host $host;    proxy_cache_bypass $http_upgrade;    }}

重启Nginx

Nginx -s reload # systemctl restart Nginx
特别注意

外部访问有可能会报502错误

原因:
SElinux配置问题
解决:
方法1.关闭SElinux
输入:sestatus,如果SElinux status: enabled ,表示开启,输入vi /etc/selinux/config 修改配置:SEliNUX=Disabled。?
方法2.将Nginx添加至SElinux的白名单

逐行执行如下命令:

yum install policycoreutils-pythoncat /var/log/audit/audit.log | grep Nginx | grep denIEd | audit2allow -M myNginxsemodule -i myNginx.pp
访问验证

总结

以上是内存溢出为你收集整理的CentOS&.NET Core初试-3-Nginx的安装和配置全部内容,希望文章能够帮你解决CentOS&.NET Core初试-3-Nginx的安装和配置所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1239609.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存