Linux Nginx安装(wget方式)

Nginx

linux 版本的安装与使用

安装

- 1. #安装编译工具及库文件
yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel


- 2. #安装 PCRE PCRE 作用是让 Nginx 支持 Rewrite 功能。
mkdir -p /usr/local/bin/pcre
cd /usr/local/bin/pcre
# https://ftp.pcre.org/pub/pcre/
wget https://ftp.pcre.org/pub/pcre/pcre-8.44.tar.gz
# 解压pcre
tar zxvf pcre-8.44.tar.gz
#进入安装包目录
cd pcre-8.44
# 配置编译
./configure
make && make install
#查看pcre版本 
pcre-config --version


- 3. #安装 Nginx 
# 下载 
mkdir -p /usr/local/nginx1
cd /usr/local/nginx1
# https://nginx.org/en/download.html
wget http://nginx.org/download/nginx-1.20.0.tar.gz
# 解压
tar vxzf nginx-1.20.0.tar.gz
# 编译
cd nginx-1.20.0
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/bin/pcre/pcre-8.44 
make && make install
# 删除旧文件 
rm -rf /usr/local/nginx1
#软链
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx

配置

# 全局模板

# user 指定运行 nginx 的用户和组(第一个参数为用户第二个为组,这里只有用户)
#user  nobody;
# 指定工作进程数(一般设置为CPU核数)
worker_processes  1;   

# 指定错误日志为 logs/ 目录下的 error.log 文件
#error_log  logs/error.log;
# 指定错误日志,并指定写入格式为 notice
#error_log  logs/error.log  notice;
# 指定错误日志,并指定写入格式为 info  
#error_log  logs/error.log  info;

# 指定 pid 文件(存放主进程 pid 号)
#pid        logs/nginx.pid;

# nginx 连接配置模块
events {
    # 指定每个工作进程最大连接数为 1024
    worker_connections  1024;
}

# http 配置模块
http {
    # 通过 include 加载 mime.types 文件,里面的 types {} 模块将文件扩展名映射到响应的 MIME 类型
    include       mime.types;
    # 定义响应的默认 MIME 类型
    default_type  application/octet-stream;

    # 写入格式 main 的内容格式如下
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    # 指定访问日志和写入格式为 main
    #access_log  logs/access.log  main;

    # 启用或者禁用 sendfile()
    sendfile        on;
    # 启用或者禁用使用套接字选项(仅在 sendfile 使用时使用)
    #tcp_nopush     on;

    # 0 值禁用保持活动的客户端连接
    #keepalive_timeout  0;
    # 65 s 超时
    keepalive_timeout  65;

    # 启用或者禁用 gzip
    #gzip  on;

    # 虚拟主机配置模块
    server {
        # 监听 80 端口
        listen       80;
        # 监听域名为 localhost
        server_name  localhost;
        # 将指定的 charset 添加到 “Content-Type” 响应头字段。如果此charset与source_charset指令中指定的charset不同,则执行转换。
        #charset koi8-r;

        # 指定该虚拟主机的访问日志
        #access_log  logs/host.access.log  main;

        # 将特定的文件或目录重新定位,如 php 文件,image 目录等
        location / {
            # 设置请求的根目录
            root   html;
            # 定义索引,按顺序匹配
            index  index.html index.htm;
        }

        # 定义显示 404 错误的 uri
        #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'
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        # 正则表达式匹配 php 文件
        #location ~ \.php$ {
            # 设置代理服务器的协议和地址,以及应该映射位置的可选URI。作为协议,可以指定“http”或“https”。该地址可以指定为一个域名或IP地址,以及一个可选端口
        #    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 服务器的地址。地址可以指定为一个域名或 IP 地址,以及一个端口
        #    fastcgi_pass   127.0.0.1:9000;
             # 设置将在以斜杠结尾的URI之后追加的文件名,
        #    fastcgi_index  index.php;
             # 设置一个应该传递给FastCGI服务器的参数。
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
             # 加载 conf/fastcgi_params 文件
        #    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
    #
    # ssl 配置,要启用 ssl 模块需要在编译 nginx 时加上 --with-http_ssl_module 参数
    #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;
    #    }
    #}

}
$ vim /usr/local/nginx/conf/nginx.conf
# jjh default
user  nginx;
worker_processes  auto;
error_log  /var/log/nginx/error.log;
pid        /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/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
              root         /usr/share/nginx/html;


        location / {
        }

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

        }

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

使用

nginx -v

域名配置

反向代理服务器

  • 支持http 和 https
# {0} 二级域名 {1} 主域名  {2} 域名脚  {3} 代理的地址 ip+port

upstream {0}_{1}_{2} { # www_baidu_com
    server {3}; # localhost:8080
    keepalive 2000;
}
server {
    listen       80;
    server_name  {0}.{1}.{2}; # www.baidu.com
    client_max_body_size 1024M;

    location / {
        proxy_pass http://{0}_{1}_{2}; # www_baidu_com
        proxy_set_header Host $host:$server_port;
    }
}


server {
    listen 443 ssl;
    #ssl on;
    server_name  {0}.{1}.{2}; # www.baidu.com
    client_max_body_size 1024M;
    ssl_certificate #pem;
    ssl_certificate_key  #key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; #使用此加密套件
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #使用该协议进行配置
    ssl_prefer_server_ciphers on;


    location / {
        proxy_pass http://{0}_{1}_{2}; # www_baidu_com
        add_header Content-Security-Policy upgrade-insecure-requests;
        proxy_set_header Host $host:$server_port;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_redirect http:// https://;
        client_max_body_size 10M;
    }
}

网站

# {0} 二级域名 {1} 主域名  {2} 域名脚  {3} 存放网站文件的目录
server {
    listen       80;
    server_name  {0}.{1}.{2}; # www.baidu.com
    client_max_body_size 1024M;

    location / {
      root  /var/project/{3}; # baidu
      index  index.html;
      try_files $uri $uri/ /index.html;
  }

}

server {
    listen    443 ssl;
    server_name  {0}.{1}.{2}; # www.baidu.com
    client_max_body_size 1024M;
    ssl_certificate  #pem;
    ssl_certificate_key  #key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;#使用此加密套件
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;  #使用该协议进行配置
    ssl_prefer_server_ciphers on;
     location / {
                root  /var/project/{3}; # baidu
                index  index.html;
                try_files $uri $uri/ /index.html;
                proxy_set_header   Host             $host:$server_port;
                proxy_set_header   X-Real-IP        $remote_addr;
                proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
                proxy_redirect https:// http://;

    }
}


版权声明:本文为weixin_41890483原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
THE END
< <上一篇
下一篇>>