Nginx 简介

1、什么是 Nginx ?

Nginx [engine x] is an HTTP and reverse proxy server, a mail proxy server, and a generic TCP/UDP proxy server, originally written by Igor Sysoev. For a long time, it has been running on many heavily loaded Russian sites including Yandex, Mail.Ru, VK, and Rambler. According to Netcraft, nginx served or proxied 25.62% busiest sites in May 2020. Here are some of the success stories: Dropbox, Netflix, Wordpress.com, FastMail.FM.

官方地址:http://nginx.org/en/

2、Nginx 服务安装

2.1、下载安装 Nginx

官方地址:https://nginx.org/en/download.html

安装环境依赖;


yum -y install gcc gcc-c++ pcre pcre-devel zlib-devel expat-devel


# 安装 gcc
yum install gcc-c++

# 安装 PCRE pcre-devel
yum install -y pcre pcre-devel

# 安装zlib
yum install -y zlib zlib-devel

# 安装Open SSL
yum install -y openssl openssl-devel

下载安装最新的 Nginx 稳定版本( 1.24.0 版本).

$ cd /usr/local/src && wget https://nginx.org/download/nginx-1.24.0.tar.gz --no-check-certificate
$ tar -xzf nginx-1.24.0.tar.gz && cd nginx-1.24.0

2.2、编译安装

#执行命令
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

#执行make命令(要是执行不成功请检查最开始安装的四个有没有安装成功)
make

#执行make install命令
make install

2.3、PCRE 编译安装

部分情况,Nginx 因为缺少 PCRE模块无法安装,且不能通过命令方式安装PCRE,则可以通过手动安装解决问题。

  • PCRE 目前最新版本为 8.45,可以点这里进行下载。 https://sourceforge.net/projects/pcre/files/pcre/ ;
  • 使用 tar -zxvf pcre-8.45.tar.gz 进行解压。
  • 运行 chmod -R 777 /pcre-8.45 对当前文件夹授予全部读写权限。
  • 切换到 /pcre-8.45 目录下,运行 ./configure 进行pcre初始化配置,会在控制台打印出一大堆的输出信息。
  • 执行 make 操作,进行编译。
  • 运行 make install,进行安装,至此PCRE安装完成。

5、开机启动

5.1、Nginx 开机启动
$ vi /lib/systemd/system/nginx.service

脚本内容:

[Unit]
Description=Nginx - high performance web server
After=nginx.service
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
Execenable=/usr/local/nginx/sbin/nginx
[Install]
WantedBy=multi-user.target

设置随机启动:

systemctl daemon-reload
# 设置开机启动
systemctl enable nginx
# 取消开机自启动
#systemctl disable nginx
# 查看服务当前状态
systemctl status nginx
# 启动nginx服务
systemctl start nginx
# 停止nginx服务
systemctl stop nginx
# 重启nginx服务
systemctl restart nginx

6、nginx.conf 修改

#user  nobody;
worker_processes  auto;
worker_rlimit_nofile 51200;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    use epoll;
    worker_connections 40960;
    multi_accept on;
}

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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml;
    gzip_vary on;
    gzip_proxied   expired no-cache no-store private auth;
    gzip_disable   "MSIE [1-6]\.";

    # include vhost
    include /usr/local/nginx/vhost/*.conf;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
作者:Jeebiz  创建时间:2024-01-12 10:22
最后编辑:Jeebiz  更新时间:2024-01-25 09:11