安装nginx,并把静态资源放入nginx

安装EPEL仓库

sudo yum install -y epel-release

安装 nginx

在安装完 EPEL 仓库后,我们就可以直接使用 yum 在安装 nginx 了,命令如下:

sudo yum install -y nginx

启动nginx

在我们实际的工作中很少直接用信号控制命令来控制 nginx 服务器的启动与重启等操作。通常我们都是通过将 nginx 作为系统服务,使用 CentOS 系统的 service命令来控制 nginx 服务的启动与重启等操作。命令如下:

# 设置为系统服务
systemctl enable nginx
# 启动
service nginx start

然后就可以使用相关命令了

systemctl nginx start/stop/reload

nginx关键文件

除了上面介绍的 nginx 常用命令,使用 nignx 另外一个需要了解的基础知识就是 nginx 服务的一些关键文件的路径和作用(PS:以下这些 nginx 关键文件的路径,都是指使用 yum 直接安装 nginx 的默认路径)

  • /etc/nginx:nginx 配置文件的根目录,nginx 的所有配置文件都在这个目录下面;
  • /etc/nginx/nginx.conf:nginx 主配置文件,所有 nginx 的基础和全局配置都应该在这个文件中配置;
  • /etc/nginx/conf.d:nginx 默认站点配置文件所在目录;
  • /var/log/nginx:nginx 日志文件目录,访问日志 access.log 和 错误日志 error.log 都在这个目录中;

防火墙配置

到目前为止,我们已经启动了 nginx 服务了,不过如果想要外网能够访问我们在 nginx 服务器中配置的 Web 站点,我们还需要做一些额外的操作————那就是做防火墙配置,让 CentOS 系统对外网公开 80 端口和允许外部访问 http 服务。命令如下:

# 开启80端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
# 允许外部访问http服务
firewall-cmd --zone=public --add-service=http --permanent
# 重启防火墙以生效
firewall-cmd --reload

如果你的 Web 站点启用 HTTPS 协议,那么你还需要让防火强对外公开 443 端口和 https 服务。

# 开启443端口
firewall-cmd --zone=public --add-port=443/tcp --permanent
# 允许访问https服务
firewall-cmd --zone=public --add-service=https --permanent
# 重启防火墙以生效
firewall-cmd --reload

selinux配置

通常,我们都会用 nginx 服务器做反响代理设置,配置 Web 站点集群,实现站点的高可用。那么这个时候我们还需要配置 selinux 配置,允许 http 服务间的通信。

# 允许http服务间的通信
setsebool -P httpd_can_network_connect 1

前端静态资源配置,并转发后端地址

配置文件如下


#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       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /data/project/energy/front/energy-front/dist;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;
        }
        location /platform-backend {
	  proxy_set_header Host $http_host;
	  proxy_set_header X-Real-IP $remote_addr;
	  proxy_set_header REMOTE-HOST $remote_addr;
	  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	  proxy_pass http://localhost:8081/platform-backend;
        }


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

}

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值