CentOS7安装及配置nginx

安装nginx

添加 nginx 官方提供的 yum 源(需要联网且时间较长)

rpm -Uvh http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.14.2-1.el7_4.ngx.x86_64.rpm

使用 yum 安装 nginx

yum install nginx

查看版本

rpm -qa | grep nginx
#我这里是nginx-1.20.1-10.el7.x86_64

yum方式安装nginx,它的安装根目录为/etc/nginx
静态资源由Nginx托管,外网必须通过正确的Url才能访问到服务器上的静态资源,而url路径跟文件的存放路径有关,可以通过修改nginx的配置文件实现自定义文件路径配置。

配置nginx

nginx的配置文件路径(以本文安装方式):

/etc/nginx/nginx.conf

默认的root节点配置:

/usr/share/nginx/html

编辑配置文件
有两个关键点
1、修改配置文件中的端口(自己开放的端口是哪个就用哪个,比如8088)
2、配置文件资源访问节点,root和alias
root:root指定的目录是上级目录,path匹配的整个路径会追加,即root+path;
alias:alias指定的目录必须带/,path匹配后面的内容会在alias指定的目录下查找,即alias+匹配到path路径后面的部分。
例:访问/usr/test/index.html的两种写法

location / { root /usr/test; index index.html; }

location /test { alias /usr/test/; index index.html; }

vi /etc/nginx/nginx.conf

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

    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       8088;
        listen       [::]:8088;
        server_name  _;
        # old dir root      /usr/share/nginx/html;
        #root        /usr/test/web/dist;
        #index       index.html;
        location / {
        root /usr/test/web/dist;
        index index.html;
        try_files $uri $uri/ /index.html;
        }
        location /test {
        alias /usr/test/webpk/dist/;
        index index.html;
        try_files $uri $uri/ /index.html;
        }
        location /h5 {
        alias /usr/test/h5/dist/;
        index index.html;
        try_files $uri $uri/ /index.html;
        }
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

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

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

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2;
#        listen       [::]:443 ssl http2;
#        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;
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}


重启 Nginx 让新的配置生效

nginx -s reload

问题及解决方法

一、如果启动Nginx服务失败报错:Job for nginx.service failed because the control process exited with error code.则通过systemctl status nginx.service查看报错信息,如果是端口占用修改配置文件为空闲端口;
二、如果报 [emerg] bind() to 0.0.0.0:XXXX failed (13: Permission denied),查看http允许访问的端口:semanage port -l | grep http_port_t并将要启动的端口加入到如上端口列表中:semanage port -a -t http_port_t -p tcp 8088;
三、在设置多地址访问页面时出现404报错,可以在location中添加:try_files $uri $uri/ /index.html;

你可能感兴趣的:(Linux,nginx,运维,服务器,centos)