阿里云服务器centos安装nginx,配置vue项目,开启nginx反向代理

centos安装nginx部署vue

centos7服务器安装配置 nginx

1、安装:yum -y install nginx

yum -y install nginx

Centos 7下安装nginx,使用yum install nginx,提示没有可用的软件包。

18 (flaskApi) [root@67 flaskDemo]# yum -y install nginx
19 已加载插件:fastestmirror, langpacks
20 Loading mirror speeds from cached hostfile
21  * base: mirrors.aliyun.com
22  * extras: mirrors.aliyun.com
23  * updates: mirrors.aliyun.com
24 没有可用软件包 nginx。
25 错误:无须任何处理

原因是nginx位于第三方的yum源里面,而不在centos官方yum源里面

很多软件包在yum里面没有的,解决的方法,就是使用epel源,也就是安装epel-release软件包。EPEL (Extra Packages for Enterprise Linux)是基于Fedora的一个项目,为“红帽系”的操作系统提供额外的软件包,适用于RHEL、CentOS等系统。可以在下面的网址上找到对应的系统版本,架构的软件包

解决办法,安装epel

sudo yum install epel-release

更新(更新时间比较长,请耐心等待)

yum update
yum 报错:保护多库版本

在执行yum update更新时,报错“保护多库版本”

解决:加–setopt=protected_multilib=false 参数

yum update --setopt=protected_multilib=false
yum安装报错——Transaction check error: conflicts with

问题:出现报错

Transaction check error:
  file /etc/libvirt/libvirt.conf from install of libvirt-libs-4.5.0-23.el7_7.6.x86_64 conflicts with file from package libvirt-client-1.1.1-29.el7.x86_64

我们可以查看一些相关信息

[root@localhost ~]# rpm -qa | grep libvirt*
[root@localhost ~]# rpm -qi libvirt-client-1.1.1-29.el7.x86_64
[root@localhost ~]# rpm -ql libvirt-client-1.1.1-29.el7.x86_64
…………省略
解决:方法是把不需要的版本删掉(低版本)
yum -y remove libvirt-client-1.1.1-29.el7.x86_64

重新试一下:

yum install -y nginx
已安装:
  nginx.x86_64 1:1.16.1-1.el7

作为依赖被安装:
  centos-indexhtml.noarch 0:7-9.el7.centos             dejavu-fonts-common.noarch 0:2.33-6.el7
  dejavu-sans-fonts.noarch 0:2.33-6.el7                fontconfig.x86_64 0:2.13.0-4.3.el7
  fontpackages-filesystem.noarch 0:1.44-8.el7          gd.x86_64 0:2.0.35-26.el7
  gperftools-libs.x86_64 0:2.6.1-1.el7                 libX11.x86_64 0:1.6.7-2.el7
  libX11-common.noarch 0:1.6.7-2.el7                   libXau.x86_64 0:1.0.8-2.1.el7
  libXpm.x86_64 0:3.5.12-1.el7                         libjpeg-turbo.x86_64 0:1.2.90-8.el7
  libxcb.x86_64 0:1.13-1.el7                           libxslt.x86_64 0:1.1.28-5.el7
  nginx-all-modules.noarch 1:1.16.1-1.el7              nginx-filesystem.noarch 1:1.16.1-1.el7
  nginx-mod-http-image-filter.x86_64 1:1.16.1-1.el7    nginx-mod-http-perl.x86_64 1:1.16.1-1.el7
  nginx-mod-http-xslt-filter.x86_64 1:1.16.1-1.el7     nginx-mod-mail.x86_64 1:1.16.1-1.el7
  nginx-mod-stream.x86_64 1:1.16.1-1.el7

完毕!

2、安装成功后nginx的几个默认目录

查看版本
$ nginx -V

image-20200717104426025

$ whereis nginx
ginx: /usr/sbin/nginx /usr/lib64/nginx /etc/nginx /usr/share/nginx /usr/share/man/man3/nginx.3pm.gz /usr/share/man/man8/nginx.8.gz

执行目录:/usr/sbin/nginx

模块所在目录:/usr/lib64/nginx/modules

配置所在目录:/etc/nginx/

默认站点目录:/usr/share/nginx/html

主要配置文件:/etc/nginx/nginx.conf 指向:/etc/nginx/conf.d/default.conf

PID目录:/var/run/nginx.pid

错误日志:/var/log/nginx/error.log

访问日志:/var/log/nginx/access.log

3、查看nginx状态(未启动前)

systemctl status nginx.service

image-20200714194609646

4、启动、停止、重载命令

systemctl start nginx.service
systemctl stop nginx.service
systemctl reload nginx.service
systemctl status nginx.service
systemctl enable nginx.service # 开机启动

注意:当配置完Nginx.conf并重新启动Nginx时,需要一定的时间才会生效。

5、查看nginx的状态及进程与端口(启动后)

systemctl status nginx.service

image-20200714195004543

以上nginx就已安装成功了

image-20200714195338363

centos7 bash: netstat: 未找到命令

查看80端口被哪个服务占用
$ netstat -antp | grep :80
netstat -antp: 未找到命令
$ yum -y install net-tools
$ netstat -antp | grep :80

image-20200715185515755

查看所有端口占用情况
$ netstat -antp | grep :

image-20200715185850896

查看nginx进程运行状态
$ ps aux | grep nginx

image-20200715190203471

查看端口被占用情况
$ lsof -i:80
-bash: lsof: 未找到命令
安装 lsof
$ yum -y install lsof

$ lsof -i:80

image-20200715190915108

杀掉进程命令
$ killall -9 nginx  // 相关nginx进行全部杀掉
$ kill -9 pid1 and kill -9 pid1 // 把PID两个进程杀掉:

server配置说明

$ whereis nginx
# /etc/nginx 
$ cd /etc/nginx # 配置文件所在目录

image-20200717124309937

$ vi nginx.conf

image-20200717124431033

添加2条server配置

image-20200718125240071

:wq保存退出编辑

重启nginx或者reload

service nginx restart
service nginx reload

nginx 出现403 Forbidden

访问 bbs.yueyouji.cn:8080,出现403的情况,解决如下

1.查看文件目录是否做默认目录(/usr/share/nginx/html)下,一般出现这个问题,是因为文件不在默认文件夹下

2.如果做usr文件夹,检查文件夹是否有777的权限,没有,则给予权限,如:chmod -R 755 / var/www

3.如果文件在 root目录下,执行:vim /etc/nginx/nginx.conf,修改文件第一行:

user root; 给nginx 用户root权限

开启反向代理 proxy_pass

通过nginx将对应域名80端口转发到3000访问node.js应用,也可以转发到其他端口比如后台是java也可以转发。

server
    {
        listen 80;
        #listen [::]:80;
        server_name domain.com ;
        location / {
                proxy_http_version 1.1;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host  $http_host;
                proxy_set_header X-Nginx-Proxy true;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_pass      http://127.0.0.1:3000;
            }
    }

精简后

vi /etc/nginx/nginx.conf
root  /usr/share/nginx/html
server{
  listen 80;
  server_name _;
  location / {
    proxy_http_version 1.1;
    proxy_pass http://127.0.0.1:3000;
  }
}

server {
  listen 80;
  server_name  _;

  root /home/web/vue/dist;
  index  index.html index.htm;

  location / {
    try_files $uri $uri/ @router;
    index  index.html index.htm;
  }
  location @router {
      rewrite ^.*$ /index.html last;
  }
  
  location ~/api/ {
    proxy_http_version 1.1;
    proxy_pass http://127.0.0.1:3000;
  }
}

配置vue项目: try_files 和 rewrite

  location / {
    try_files $uri $uri/ @router;
    index  index.html index.htm;
  }
  location @router {
      rewrite ^.*$ /index.html last;
  }
nginx权限问题failed(13:Permission denied)

打开nginx.conf

user nginx; 修改为 user root;

最终nginx配置文件

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    log_format  vue-app '$remote_addr,$time_iso8601,$request,'
                      '$http_referer,$http_x_forwarded_for';
    access_log  /var/log/nginx/access.log  vue-app;

    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;

    server {
        listen 80 default_server;
        server_name  _;

        root   /usr/share/nginx/html/vue-app;
        index  index.html index.htm;

        location / {
            try_files $uri $uri/ @router;
            index  index.html index.htm;
        }
        location @router {
            rewrite ^.*$ /index.html last;
        }

        location ~ /api/ {
            proxy_http_version 1.1;
            proxy_pass http://127.0.0.1:3000;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers PROFILE=SYSTEM;
#        ssl_prefer_server_ciphers on;
#
#        # 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 {
#        }
#    }

}

阿里云部署多个应用后

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
# include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    log_format  vue-app '$remote_addr,$time_iso8601,$request,'
                      '$http_referer,$http_x_forwarded_for';

    access_log  /var/log/nginx/access.log  vue-app;

    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;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        # root         /root/web/vue-app;

        # Load configuration files for the default server block.
        # include /etc/nginx/default.d/*.conf;

        location / {
          root  /root/web/vue-app;
          try_files $uri $uri/ @router;
          index  index.html index.htm;
        }

        location /backend {
          alias  /root/web/vue-app-backend/dist;
          try_files $uri $uri/ @router;
          index  index.html index.htm;
        }

        location @router {
           rewrite ^.*$ /index.html last;
        }

        location ~ /api/ {
           proxy_pass http://127.0.0.1:3000;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_set_header Host $http_host;
           proxy_set_header X-Nginx-Proxy true;
           proxy_set_header Connection "";
        }

        location ~ /api2/ {
           proxy_pass http://127.0.0.1:3001;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_set_header Host $http_host;
           proxy_set_header X-Nginx-Proxy true;
           proxy_set_header Connection "";
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}

猜你喜欢

转载自blog.csdn.net/zhouweihua138/article/details/129543135