声明
本文翻译自:
Host ASP.NET Core application on Linux with Nginx
Apache Mod_proxy '[Error] (13)Permission Denied' Error on RHEL
1.安装epel:
如果直接安装Nginx,可能会提示没有可用软件包
yum install epel-release
2.安装Nginx
sudo yum install nginx
3.运行Nginx
sudo service nginx start
4.查看Nginx运行状态
systemctl status nginx.service
5.启动Nginx
systemctl start nginx #启用Nginx
systemctl enable nginx #设置开机启动
此时可以通过浏览器运行localhost查看Nginx状态
6.配置Nginx做反向代理
如果将Nginx作为ASP.NET Core应用请求的反向代理,需要需改配置文件
sudo vi /etc/nginx/nginx.conf
将配置文件中的Server部分内容修改如下:
server {
listen 80;
server_name MvcDemoApp;
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 $http_host;
proxy_cache_bypass $http_upgrade;
}
}
测试Nginx配置文件是否存在语法问题(因为少写过一个;,这个是可以提示的)
sudo nginx -t
重新加载nginx配置文件
sudo nginx -s reload
7.502 Bad Gateway
按照上文操作正常的话,在服务器的浏览器查看80端口:http://localhost。
会报502的错误,这是原文没有提到的,解决办法如下:
/usr/sbin/setsebool httpd_can_network_connect 1
但是指令是动态的设置,如果指令运行后问题可以解决,请键入以下指令:
/usr/sbin/setsebool -P httpd_can_network_connect 1
参考
Linux下安装Nginx并部署Net Core Web API程序
How To Install Nginx on CentOS 7