Nginx 安装配置

打印 上一主题 下一主题

主题 640|帖子 640|积分 1920

Nginx安装配置详解、Nginx配置https反向代理示例
Nginx 安装

软硬件环境:CentOS 7.6_64位
安装依赖环境
  1. yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel pcre pcre-devel
复制代码
查看pcre版本
  1. [root@yaenli pcre]# pcre-config --version
复制代码
安装Nginx


  • 下载Nginx安装包:手动从官网地址 nginx: download,下载安装包上传至服务器目录或者采用如下命令
    1. [root@yaenli src]# cd /usr/local/src/
    2. [root@yaenli src]# wget http://nginx.org/download/nginx-1.22.0.tar.gz
    复制代码
  • 解压安装包
    1. [root@yaenli src]# tar zxvf nginx-1.22.0.tar.gz
    复制代码
  • 进入安装目录执行配置命令创建makefile文件
    1. [root@yaenli src]# cd nginx-1.22.0
    2. ./configure \
    3. --prefix=/usr/local/nginx \
    4. --with-http_stub_status_module \
    5. --with-http_ssl_module \
    6. --with-http_v2_module \
    7. --with-pcre
    复制代码
    注1:\ 代表在命令行中换行,用于提高可读性
    注2:configure参数官方说明:Building nginx from Sources
    部分模块说明:(后续可以根据需要依据此参数增减模块)
    http_ssl_module:用于支持https
    http_v2_module:用于支持HTTP/2
    配置结果:
    1. Configuration summary
    2.   + using system PCRE library
    3.   + using system OpenSSL library
    4.   + using system zlib library
    5.   nginx path prefix: "/usr/local/nginx"
    6.   nginx binary file: "/usr/local/nginx/sbin/nginx"
    7.   nginx modules path: "/usr/local/nginx/modules"
    8.   nginx configuration prefix: "/usr/local/nginx/conf"
    9.   nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
    10.   nginx pid file: "/usr/local/nginx/logs/nginx.pid"
    11.   nginx error log file: "/usr/local/nginx/logs/error.log"
    12.   nginx http access log file: "/usr/local/nginx/logs/access.log"
    13.   nginx http client request body temporary files: "client_body_temp"
    14.   nginx http proxy temporary files: "proxy_temp"
    15.   nginx http fastcgi temporary files: "fastcgi_temp"
    16.   nginx http uwsgi temporary files: "uwsgi_temp"
    17.   nginx http scgi temporary files: "scgi_temp"
    复制代码
  • 编译安装
    1. [root@yaenli nginx-1.22.0]# make && make install
    复制代码
  • 查看Nginx版本
    1. [root@yaenli nginx-1.22.0]# /usr/local/nginx/sbin/nginx -V
    复制代码
    至此,安装完成。
Nginx 配置

使用NGINXConfig完成配置工作。
NGINXConfig是一个在线的可视化的Nginx配置工具,地址:NGINXConfig | DigitalOcean
使用配置方法:

  • 进入你的 NGINX服务器上的配置目录:
    1. cd /usr/local/nginx/conf
    复制代码
  • 创建当前NGINX配置的备份:
    1. tar -czvf nginx_$(date +'%F_%H-%M-%S').tar.gz nginx.conf sites-available/ sites-enabled/ nginxconfig.io/
    复制代码
  • 使用tar解压新的配置文件(从NGINXConfig上下载)
    1. tar -xzvf nginxconfig.io-example.com.tar.gz | xargs chmod 0644
    复制代码
  • 服务器上运行此命令生成Diffie-Hellman keys:
    1. openssl dhparam -out /usr/local/nginx/conf/dhparam.pem 2048
    复制代码
  • 重新加载NGINX以载入新的配置:
    1. sudo nginx -t && sudo systemctl reload nginx
    复制代码
https的反向代理配置示例

单应用的https反向代理,采用模块化配置方法Nginx配置示例
示例的具体配置文件内容如下:

  • /usr/local/nginx/conf/nginx.conf
  1. # Generated by nginxconfig.io
  2. # See nginxconfig.txt for the configuration share link
  3. user                 root;
  4. pid                  /run/nginx.pid;
  5. worker_processes     auto;
  6. worker_rlimit_nofile 65535;
  7. # Load modules
  8. include              /usr/local/nginx/conf/modules-enabled/*.conf;
  9. events {
  10.     multi_accept       on;
  11.     worker_connections 65535;
  12. }
  13. http {
  14.     charset                utf-8;
  15.     sendfile               on;
  16.     tcp_nopush             on;
  17.     tcp_nodelay            on;
  18.     server_tokens          off;
  19.     log_not_found          off;
  20.     types_hash_max_size    2048;
  21.     types_hash_bucket_size 64;
  22.     client_max_body_size   16M;
  23.     # MIME
  24.     include                mime.types;
  25.     default_type           application/octet-stream;
  26.     # Logging
  27.     access_log             logs/access.log;
  28.     error_log              logs/error.log warn;
  29.     # SSL
  30.     ssl_session_timeout    1d;
  31.     ssl_session_cache      shared:SSL:10m;
  32.     ssl_session_tickets    off;
  33.     # Diffie-Hellman parameter for DHE ciphersuites
  34.     ssl_dhparam            /usr/local/nginx/conf/dhparam.pem;
  35.     # Mozilla Intermediate configuration
  36.     ssl_protocols          TLSv1.2 TLSv1.3;
  37.     ssl_ciphers            ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
  38.     # OCSP Stapling
  39.     ssl_stapling           on;
  40.     ssl_stapling_verify    on;
  41.     resolver               1.1.1.1 1.0.0.1 8.8.8.8 8.8.4.4 208.67.222.222 208.67.220.220 valid=60s;
  42.     resolver_timeout       2s;
  43.     # Connection header for WebSocket reverse proxy
  44.     map $http_upgrade $connection_upgrade {
  45.         default upgrade;
  46.         ""      close;
  47.     }
  48.     map $remote_addr $proxy_forwarded_elem {
  49.         # IPv4 addresses can be sent as-is
  50.         ~^[0-9.]+$        "for=$remote_addr";
  51.         # IPv6 addresses need to be bracketed and quoted
  52.         ~^[0-9A-Fa-f:.]+$ "for="[$remote_addr]"";
  53.         # Unix domain socket names cannot be represented in RFC 7239 syntax
  54.         default           "for=unknown";
  55.     }
  56.     map $http_forwarded $proxy_add_forwarded {
  57.         # If the incoming Forwarded header is syntactically valid, append to it
  58.         "~^(,[ \\t]*)*([!#$%&'*+.^_`|~0-9A-Za-z-]+=([!#$%&'*+.^_`|~0-9A-Za-z-]+|"([\\t \\x21\\x23-\\x5B\\x5D-\\x7E\\x80-\\xFF]|\\\\[\\t \\x21-\\x7E\\x80-\\xFF])*"))?(;([!#$%&'*+.^_`|~0-9A-Za-z-]+=([!#$%&'*+.^_`|~0-9A-Za-z-]+|"([\\t \\x21\\x23-\\x5B\\x5D-\\x7E\\x80-\\xFF]|\\\\[\\t \\x21-\\x7E\\x80-\\xFF])*"))?)*([ \\t]*,([ \\t]*([!#$%&'*+.^_`|~0-9A-Za-z-]+=([!#$%&'*+.^_`|~0-9A-Za-z-]+|"([\\t \\x21\\x23-\\x5B\\x5D-\\x7E\\x80-\\xFF]|\\\\[\\t \\x21-\\x7E\\x80-\\xFF])*"))?(;([!#$%&'*+.^_`|~0-9A-Za-z-]+=([!#$%&'*+.^_`|~0-9A-Za-z-]+|"([\\t \\x21\\x23-\\x5B\\x5D-\\x7E\\x80-\\xFF]|\\\\[\\t \\x21-\\x7E\\x80-\\xFF])*"))?)*)?)*$" "$http_forwarded, $proxy_forwarded_elem";
  59.         # Otherwise, replace it
  60.         default "$proxy_forwarded_elem";
  61.     }
  62.     # Load configs
  63.     include /usr/local/nginx/conf/conf.d/*.conf;
  64.     include /usr/local/nginx/conf/sites-enabled/*;
  65. }
复制代码

  • /usr/local/nginx/conf/sites-enabled/example.com.conf
  1. server {
  2.     listen              443 ssl;
  3.     listen              [::]:443 ssl;
  4.     server_name         example.com;
  5.     # SSL
  6.     ssl_certificate     /usr/local/nginx/conf/ssl/example.com.crt;
  7.     ssl_certificate_key /usr/local/nginx/conf/ssl/example.com.key;
  8.     # security
  9.     include             nginxconfig.io/security.conf;
  10.     # logging
  11.     access_log          logs/example.com.access.log;
  12.     error_log           logs/example.com.error.log warn;
  13.     # reverse proxy
  14.     location / {
  15.         proxy_pass            http://127.0.0.1:3000;
  16.         proxy_set_header Host $host;
  17.         include               nginxconfig.io/proxy.conf;
  18.     }
  19.     # additional config
  20.     include nginxconfig.io/general.conf;
  21. }
  22. # HTTP redirect
  23. server {
  24.     listen      80;
  25.     listen      [::]:80;
  26.     server_name example.com;
  27.     return      301 https://example.com$request_uri;
  28. }
复制代码

  • /usr/local/nginx/conf/nginxconfig.io/security.conf
  1. # security headers
  2. add_header X-XSS-Protection        "1; mode=block" always;
  3. add_header X-Content-Type-Options  "nosniff" always;
  4. add_header Referrer-Policy         "no-referrer-when-downgrade" always;
  5. add_header Content-Security-Policy "default-src 'self' http: https: ws: wss: data: blob: 'unsafe-inline'; frame-ancestors 'self';" always;
  6. add_header Permissions-Policy      "interest-cohort=()" always;
  7. # . files
  8. location ~ /\.(?!well-known) {
  9.     deny all;
  10. }
复制代码

  • /usr/local/nginx/conf/nginxconfig.io/general.conf
  1. # favicon.ico
  2. location = /favicon.ico {
  3.     log_not_found off;
  4.     access_log    off;
  5. }
  6. # robots.txt
  7. location = /robots.txt {
  8.     log_not_found off;
  9.     access_log    off;
  10. }
  11. # gzip
  12. gzip            on;
  13. gzip_vary       on;
  14. gzip_proxied    any;
  15. gzip_comp_level 6;
  16. gzip_types      text/plain text/css text/xml application/json application/javascript application/rss+xml application/atom+xml image/svg+xml;
复制代码

  • /usr/local/nginx/conf/nginxconfig.io/proxy.conf
  1. proxy_http_version                 1.1;
  2. proxy_cache_bypass                 $http_upgrade;
  3. # Proxy SSL
  4. proxy_ssl_server_name              on;
  5. # Proxy headers
  6. proxy_set_header Upgrade           $http_upgrade;
  7. proxy_set_header Connection        $connection_upgrade;
  8. proxy_set_header X-Real-IP         $remote_addr;
  9. proxy_set_header Forwarded         $proxy_add_forwarded;
  10. proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;
  11. proxy_set_header X-Forwarded-Proto $scheme;
  12. proxy_set_header X-Forwarded-Host  $host;
  13. proxy_set_header X-Forwarded-Port  $server_port;
  14. # Proxy timeouts
  15. proxy_connect_timeout              60s;
  16. proxy_send_timeout                 60s;
  17. proxy_read_timeout                 60s;
复制代码

  • /usr/local/nginx/conf/nginxconfig.txt
  1. https://www.digitalocean.com/community/tools/nginx?domains.0.server.documentRoot=%2Fnccloud&domains.0.server.redirectSubdomains=false&domains.0.https.http2=false&domains.0.https.hsts=false&domains.0.https.certType=custom&domains.0.php.php=false&domains.0.reverseProxy.reverseProxy=true&domains.0.routing.root=false&domains.0.routing.index=index.html&domains.0.routing.fallbackHtml=true&domains.0.logging.accessLog=true&domains.0.logging.errorLog=true&global.logging.accessLog=logs%2Faccess.log&global.logging.errorLog=logs%2Ferror.log%20warn&global.nginx.nginxConfigDirectory=%2Fusr%2Flocal%2Fnginx%2Fconf%2F&global.nginx.user=root&global.tools.symlinkVhost=false&global.app.lang=zhCN
复制代码
检查配置文件正确性
  1. [root@yaenli conf]# /usr/local/nginx/sbin/nginx -t
复制代码
Nginx 启停
  1. /usr/local/nginx/sbin/nginx                           # 启动 Nginx
  2. /usr/local/nginx/sbin/nginx -s reload            # 重新载入配置文件
  3. /usr/local/nginx/sbin/nginx -s reopen            # 重启 Nginx
  4. /usr/local/nginx/sbin/nginx -s stop              # 停止 Nginx
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

渣渣兔

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表