Nginx安装及配置

nginx编译安装及配置

nginx下载官网:nginx: download

关闭防火墙和selinux

[root@localhost ~]# wget -P /usr/local/src/ http://nginx.org/download/nginx-1.20.1.tar.gz
[root@localhost ~]# yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel
openssl openssl-devel perl-devel perl-ExtUtils-Embed gd-devel
[root@localhost ~]# useradd -s /sbin/nologin -M nginx
[root@localhost ~]# mkdir -p /var/tmp/nginx/client/
[root@localhost ~]# cd /usr/local/src/
[root@localhost src]# tar -zvxf nginx-1.20.1.tar.gz
[root@localhost ~]# cd nginx-1.20.1
[root@localhost nginx-1.20.1]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --sbin-path=/usr/sbin/nginx --error-log-path=/var/log/nginx/nginx_error.log --http-log-path=/var/log/nginx/nginx_access.log --pid-path=/usr/local/nginx/run/nginx.pid --lock-path=/usr/local/nginx/lock/nginx --with-http_image_filter_module --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_perl_module --with-mail --with-mail_ssl_module --with-pcre --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fcgi --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi
[root@localhost nginx-1.20.1]# make && make install

systemd添加nginx服务

(或启动脚本形式,自行百度)

[root@localhost ~]# vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/usr/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

启动nginx服务
[root@localhost ~]# pkill nginx
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl start nginx
[root@localhost ~]# systemctl status nginx
[root@localhost ~]# systemctl enable nginx

主配置文件配置

[root@localhost nginx]# mkdir /etc/nginx/conf.d
[root@localhost nginx]# vi /etc/nginx/nginx.conf
user  nginx;
worker_processes  1;
error_log  /var/log/nginx/nginx1-error.log;
pid        /usr/local/nginx/run/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    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/nginx1-access.log  main;
    sendfile        on;
    tcp_nopush     on;
    keepalive_timeout  65;
    gzip  on;
    include /etc/nginx/conf.d/*.conf;
}

子配置文件

/etc/nginx/conf.d/test.conf

每次修改完需重载配置文件或重启

# /usr/sbin/nginx -s reoad

或
# /usr/sbin/nginx -s stop
# /usr/sbin/nginx
##重启失败,尝试pkill nginx

基于域名配置虚拟主机

[root@localhost nginx]# vim /etc/nginx/conf.d/test.conf
#虚拟主机1
server {
        listen 80;
        server_name www.test.com;
        access_log /var/log/nginx/test-access.log main;
        error_log /var/log/nginx/test-error.log;
        location /{
                root /var/www/test;
                index index.html index.htm index.php;

        #       stub_status on;
        #       access_log off;
        }


#       location /status{
#       stub_status on;
#       access_log off;
#       }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        root /var/www/test;
        }


}
#虚拟机主机2,用于查看HTTP的状态,需要--with-http_stub_status_module模块
server{
    listen 80;
    server_name www.status.com;
    location /{
        stub_status on;
        access_log off;
    }
}

#虚拟主机3,这里根目录与主机1相同,自行修改
server {
        listen 80;
        server_name www.test2.com;
        access_log /var/log/nginx/test2-access.log main;
        error_log /var/log/nginx/test2-error.log;
        location /{
                root /var/www/test;
                index index.html index.htm index.php;
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        root /var/www/test;
        }
}
修改完需手动创建根目录

状态信息配置

给虚拟主机设置状态,需要--with-http_stub_status_module模块

三种设置方法

[root@localhost nginx]# vim /etc/nginx/conf.d/test.conf
#虚拟主机1
server {
        listen 80;
        server_name www.test.com;
        access_log /var/log/nginx/test-access.log main;
        error_log /var/log/nginx/test-error.log;
        location /{
                root /var/www/test;
                index index.html index.htm index.php;
#----------------------位置1,location下添加两行,但这样不能直接访问index.html-------------
        #       stub_status on;
        #       access_log off;
#---------------------------------------------------------------------
        }

#-------------------位置2,访问根目录下的status-----------------------------------
#       location /status{
#       stub_status on;
#       access_log off;
#       }
#-----------------------------------------------------------------------------
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        root /var/www/test;
        }
}


#-----------------位置3,新建一个虚拟主机-------------------------------------
#虚拟机主机2,用于查看HTTP的状态,需要--with-http_stub_status_module模块
server{
    listen 80;
    server_name www.status.com;
    location /{
        stub_status on;
        access_log off;
    }
}
#-------------------------------------------------------------------------

基于ip

[root@localhost nginx]# ifconfig ens33:1 192.168.10.10
[root@localhost nginx]# ip a
ens33: 192.168.10.11
ens33:1 192.168.10.10
[root@localhost nginx]# vim /etc/nginx/conf.d/xxx.conf
server {
        listen 192.168.10.11:80;
        server_name www.test.com;
        access_log /var/log/nginx/test-access.log main;
        error_log /var/log/nginx/test-error.log;
        location /{
                root /var/www/test;
                index index.html index.htm index.php;
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        root /var/www/test;
        }
}

server {
        listen 192.168.10.10:80;
        server_name www.test2.com;
        access_log /var/log/nginx/test2-access.log main;
        error_log /var/log/nginx/test2-error.log;
        location /{
                root /var/www/test2;
                index index.html index.htm index.php;
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        root /var/www/test2;
        }
}

# // ifconfig eth0:1 192.168.95.100 down 删除绑定的IP命令

基于端口

server {
        listen 80;
        server_name www.test.com;
        access_log /var/log/nginx/test-access.log main;
        error_log /var/log/nginx/test-error.log;
        location /{
                root /var/www/test;
                index index.html index.htm index.php;
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        root /var/www/test;
        }
}

#主机2,访问方式:www.test2.com:8080
server {
        listen 8080;
        server_name www.test2.com;
        access_log /var/log/nginx/test2-access.log main;
        error_log /var/log/nginx/test2-error.log;
        location /{
                root /var/www/test2;
                index index.html index.htm index.php;
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        root /var/www/test2;
        }
}

访问权限

server {
        listen 80;
        server_name www.test.com;
        access_log /var/log/nginx/test-access.log main;
        error_log /var/log/nginx/test-error.log;
        location /{
                root /var/www/test;
                index index.html index.htm index.php;
                deny 192.168.0.10; # 拒绝192.168.0.10
                deny 192.168.133.0/24; #拒绝这个网段访问
				allow all;
                
        }

}

Nginx安装及配置

上一篇:php5.4.26 安装出现 error: ‘struct gdIOCtx’ has no member named ‘data’


下一篇:标签的使用 JSTL