【环境部署】华为云ECS安装配置Nginx(CentOS 7.4)

华为云CentOS 7.4 安装Nginx1.15.8

4.1 前提

Nginx官网:http://nginx.org/

由于Nginx模块依赖一些类库,所以在安装Nginx之前,必须先安装这些lib库,主要有如下几个安装命令,安装过程的提示命令都直接输入【y】继续安装即可。

yum install gcc-c++  
yum install pcre pcre-devel  
yum install zlib zlib-devel  
yum install openssl openssl-devel 

4.2 安装Nginx

4.2.1 下载

wget http://nginx.org/download/nginx-1.15.8.tar.gz 

4.2.2 解压文件

tar -zxvf nginx-1.15.8.tar.gz

4.2.3 安装

./configure && make && make install

注意:默认安装在/usr/local/nginx, 使用–prefix参数指定nginx安装的目录。可以使用命令【whereis nginx】来查看安装目录。

4.3 启动

安装完毕后,进入安装后目录(/usr/local/nginx)便可以启动或停止。

/usr/local/nginx/sbin/nginx

/usr/local/nginx/sbin/nginx -s stop 

在这里插入图片描述

设置开启启动,文件末尾添加启动脚本(/usr/local/nginx/sbin/nginx):

chmod 755 /etc/rc.d/rc.local
vim /etc/rc.d/rc.local

4.4 注意事项

  • 华为云ECS服务器默认不开启80端口,直接访问存在问题。需要进入华为官网云控制台中安全组添加(入规则:80)。
  • 华为云ECS默认开启了防火墙,linux防火墙禁止访问80端口。

确认Nginx已经启动并监听了80端口

[root@ecs-s6 home]# netstat -anp | grep :80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      11309/nginx: master 

确认Linux防火墙禁止访问80端口。

[root@ecs-s6 logs]# firewall-cmd --query-port=80/tcp
no

解决方式:

# 1 防火墙列表中允许80端口

firewall-cmd --permanent --add-port=80/tcp

# 修改配置后重新启动才生效

firewall-cmd --reload 


# 2 关闭防火墙

systemctl stop firewalld  

上述设定后再次访问Nginx地址,可以显示欢迎页,访问OK。

4.5 反向代理配置

​ 反向代理(Reverse Proxy)方式是指Nginx以代理服务器来接受网络上的连接请求,然后将请求转发给内部网络上的服务器,并将从内部服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。当外部客户端尝试访问内容服务器时,会将其送到代理服务器。实际内容位于内容服务器上,在防火墙内部受到安全保护,所以说反向代理服务器也起到了安全防护作用。反向代理服务器还可以实现负载均衡。

4.5.1 VUE静态页面部署

我们在服务器上部署一个前端的VUE应用(其实就是找一个目录,拷贝代码即可),现在主要是静态页面,当我们访问这个机器的指定端口时,希望能够看到这个默认的静态页面。

VUE应用部署:/home/meetmng/vueweb

[root@ecs-s6 vueweb]# ll
total 12
-rw-r--r-- 1 root root  922 Jan 13 15:19 meetingDetail.html
-rw-r--r-- 1 root root  910 Jan 13 15:18 meeting.html
drwxr-xr-x 9 root root 4096 Jan 13 15:19 static
[root@ecs-s6 vueweb]# 


4.5.2 nginx.conf配置转发

打开Nginx安装目录下面的配置文件:/usr/local/nginx/conf/nginx.conf

我们这个应用比较简单,默认首页访问地址基本修改内容如下:

        # 原来的默认内容
		location / {
            root   html;
            index  index.html index.htm;
        }
        # 修改后内容
        location / {
            root   /home/meetmng/vueweb;
            index  meeting.html;
        }

前端VUE画面中调用SpringBoot的RestFul接口配置如下:

		# 添加接口的映射内容
		location /gavin/api/confmng {
            proxy_pass              http://localhost:9109/gavin/api/confmng;
            proxy_redirect          default;
            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,访问页面可以看到静态页面能够正常加载。

4.6 nginx.conf配置文件说明

## 运行用户和用户组,默认关闭,如果没有用户可以指定root
#user  nobody;
## 启动进程,通常设定和cpu核数相等
worker_processes  1;

## 全局错误日志,可以指定错误级别
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

## PID文件位置,在worker_processes>1 的情况下会有多个进程,
## 但是管理进程只有一个
#pid        logs/nginx.pid;


events {
	## 每个进程可同时建立的链接数量 
    worker_connections  1024;
}


http {
	## ========Nginx后端服务配置项 Start =========
    location /gavin/api/confmng {
        proxy_pass              http://localhost:9109/gavin/api/confmng;
        proxy_redirect          default;
        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后端服务配置项 End   =========
	
	# 文件扩展名与文件类型映射表
    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指令指定nginx是否调用sendfile函数来输出文件,
	# 对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,
    # 可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
    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   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;
    #    }
    #}

}

发布了37 篇原创文章 · 获赞 24 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/gavinbj/article/details/104094476