2020-11-14 centos 7.5 安装ngnix

本文在centos7.5下安装ngnix

  1. 查看CentOS版本
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
  1. 安装nginx
    yum -y install nginx # 安装 nginx
[root@localhost ~]# yum -y install nginx
已加载插件:fastestmirror
Determining fastest mirrors
 * base: mirrors.huaweicloud.com
 * centos-sclo-rh: mirrors.huaweicloud.com
 * centos-sclo-sclo: mirrors.huaweicloud.com
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.huaweicloud.com
base                                                                                                                                                                | 3.6 kB  00:00:00     
centos-sclo-rh                                                                                                                                                      | 3.0 kB  00:00:00     
centos-sclo-sclo                                                                                                                                                    | 3.0 kB  00:00:00     
extras                                                                                                                                                              | 2.9 kB  00:00:00     
updates                                                                                                                                                             | 2.9 kB  00:00:00     
(1/5): extras/7/x86_64/primary_db                                                                                                                                   | 222 kB  00:00:00     
(2/5): centos-sclo-sclo/x86_64/primary_db                                                                                                                           | 297 kB  00:00:00     
(3/5): centos-sclo-rh/x86_64/primary_db                                                                                                                             | 2.9 MB  00:00:01     
(4/5): base/7/x86_64/primary_db                                                                                                                                     | 6.1 MB  00:00:01     
(5/5): updates/7/x86_64/primary_db                                                                                                                                  | 2.5 MB  00:00:03     
没有可用软件包 nginx。
错误:无须任何处理

原因是nginx位于第三方的yum源里面,而不在centos官方yum源里面
安装
[root@localhost ~]# yum install epel-release
再更新下
[root@localhost ~]# yum update
[root@localhost ~]# yum -y install nginx
使用 yum 进行 Nginx 安装时,Nginx 配置文件在/etc/nginx目录下

$ sudo systemctl enable nginx # 设置开机启动 
$ sudo service nginx start # 启动 nginx 服务
$ sudo service nginx stop # 停止 nginx 服务
$ sudo service nginx restart # 重启 nginx 服务
$ sudo service nginx reload # 重新加载配置,一般是在修改过 nginx 配置文件时使用。

访问80端口


成功页面

修改配置,部署vue应用

# 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"';

    access_log  /var/log/nginx/access.log  main;

    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;
    upstream admin_backend {
        server 127.0.0.1:8080       weight=10;
    }
    server {
        listen       80 default_server;
        server_name  locahost;
        root         /servers/labor-admin-ui;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        location / {
            try_files $uri $uri/ /index.html;
            index  index.html index.htm;
        }
        
        location /api/ {
            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:8080/;
            
        }

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

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root   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 HIGH:!aNULL:!MD5;
#        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 = /404.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#        location = /50x.html {
#        }
#    }

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容