centos7安装与配置nginx

安装nginx:

sudo yum install nginx

 

设置nginx开机自动启动:

sudo systemctl enable nginx

 

启动nginx:

sudo systemctl start nginx

 

打开防火墙80(HTTP)和443(HTTPS)端口:

sudo firewall-cmd --permanent --zone=public --add-service=http

sudo firewall-cmd --permanent --zone=public --add-service=https

sudo firewall-cmd --reload

 

通过上面安装方式安装的nginx后,配置文件都在这里/etc/nginx/

 

                                 

一些好的实践约定:

/etc/nginx/

           conf.common.d/

                        name1.conf

                                                    name2.conf

           conf.d/

                                      web1.com.8001.conf

                                      web2.com.8002.conf

1.创建目录:/etc/nginx/conf.common.d/

    对于配置中重复可以服用的配置,可以分门别类的放到这个目录,需要的时候就可以在文件中引用方便修改管理

2.每个独立的nginx配置都放在 目录 /etc/nginx/conf.d/

  基于维护简便的考虑,我们为每个独立的web站点(服务)创建单独的配置文件,以命名"域名+端口.conf"的形式存于目录/etc/nginx/conf.d/下

3.除了配置文件外,日志相关文件存于/var/log/nginx/目录,如access.log 和 error.log,比较好的做法是为上面每个站点服务创建不同的访问权限和错误日志文件,这样查找错误时,会更加方便,比如站点web1.com.8001的日志文件: /var/log/nginx/web1.com.8001.log

centos7安装与配置nginx_第1张图片

 

4.将要部署的代码文件,可以遵循下面目录结构存放

/usr/share/

          nginx/

               html/

/var/www/

            web1.com.8001/

            web2.com.8002/

 

 

4.1 默认的80端口nginx服务html存放的文件在/usr/share/nginx/html

4.2 将要部署的代码文件存放的文件可以根据站点名分别存放在/var/www/域名或站点名+端口/ 下  (如上所示)

 

 

 nginx主配置文件:/etc/nginx/nginx.conf

参考:以下是nginx初始安装时的配置的原始文件内容 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 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         /usr/share/nginx/html;

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

# 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 = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

 

你可能感兴趣的:(nginx)