ToB企服应用市场:ToB评测及商务社交产业平台

标题: Nginx 安装配置 [打印本页]

作者: 渣渣兔    时间: 2022-10-12 22:52
标题: Nginx 安装配置
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 配置

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

单应用的https反向代理,采用模块化配置方法Nginx配置示例
示例的具体配置文件内容如下:
  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. }
复制代码
  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. }
复制代码
  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. }
复制代码
  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;
复制代码
  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;
复制代码
  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
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4