Ali 클라우드 서버 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가 centos 공식 yum 소스가 아닌 타사 yum ​​소스에 있기 때문입니다.

많은 소프트웨어 패키지는 yum에서 사용할 수 없습니다. 해결책은 epel 소스를 사용하는 것입니다. 즉, epel-release 패키지를 설치하십시오. EPEL(Extra Packages for Enterprise Linux)은 RHEL, CentOS 및 기타 시스템에 적합한 "Red Hat" 운영 체제용 추가 소프트웨어 패키지를 제공하는 Fedora 기반 프로젝트입니다. 해당 시스템 버전 및 아키텍처 소프트웨어 패키지는 다음 웹사이트에서 찾을 수 있습니다.

해결책은 epel을 설치하는 것입니다.

sudo yum install epel-release

업데이트(업데이트 시간이 상대적으로 깁니다. 기다려 주십시오.)

yum update
yum 오류: 다중 라이브러리 버전 보호

yum 업데이트를 실행할 때 "여러 데이터베이스 버전 보호" 오류가 보고됨

솔루션: –setopt=protected_multilib=false 매개변수 추가

yum update --setopt=protected_multilib=false
Yum 설치 오류 - 트랜잭션 확인 오류: 충돌

문제: 오류가 발생합니다

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

이미지-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

이미지-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

이미지-20200714195004543

위의 nginx가 성공적으로 설치되었습니다.

이미지-20200714195338363

centos7 bash: netstat: 명령을 찾을 수 없음

포트 80을 차지하는 서비스 확인
$ netstat -antp | grep :80
netstat -antp: 未找到命令
$ yum -y install net-tools
$ netstat -antp | grep :80

이미지-20200715185515755

모든 포트 사용량 보기
$ netstat -antp | grep :

이미지-20200715185850896

nginx 프로세스의 실행 상태 보기
$ ps aux | grep nginx

이미지-20200715190203471

포트 사용량 확인
$ lsof -i:80
-bash: lsof: 未找到命令
lsof 설치
$ yum -y install lsof

$ lsof -i:80

이미지-20200715190915108

kill 프로세스 명령
$ killall -9 nginx  // 相关nginx进行全部杀掉
$ kill -9 pid1 and kill -9 pid1 // 把PID两个进程杀掉:

서버 구성 지침

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

이미지-20200717124309937

$ vi nginx.conf

이미지-20200717124431033

2개의 서버 구성 추가

이미지-20200718125240071

:wq 저장하고 편집 종료

nginx를 다시 시작하거나 다시 로드

service nginx restart
service nginx reload

403 Forbidden이 nginx에 나타납니다.

bbs.yueyouji.cn:8080, 403을 방문하면 해결 방법은 다음과 같습니다.

1. 파일 디렉토리가 기본 디렉토리(/usr/share/nginx/html)에 있는지 확인 일반적으로 파일이 기본 폴더에 없기 때문에 이러한 문제가 발생합니다.

2. usr 폴더를 만드는 경우 폴더에 777 권한이 있는지 확인하고 그렇지 않은 경우 chmod -R 755 /var/www와 같이 권한을 부여합니다.

3. 파일이 루트 디렉터리에 있는 경우 vim /etc/nginx/nginx.conf를 실행하고 파일의 첫 번째 줄을 수정합니다.

사용자 루트; nginx 사용자 루트 권한 부여

리버스 프록시 proxy_pass 활성화

해당 도메인 이름의 포트 80을 3000으로 포워딩하여 nginx를 통해 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 및 다시 쓰기

  location / {
    try_files $uri $uri/ @router;
    index  index.html index.htm;
  }
  location @router {
      rewrite ^.*$ /index.html last;
  }
nginx 권한 문제 실패(13:권한 거부됨)

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

}

Alibaba Cloud가 여러 애플리케이션을 배포한 후

# 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