{"id":12821,"date":"2021-02-27T10:37:23","date_gmt":"2021-02-27T02:37:23","guid":{"rendered":"http:\/\/blog.idc.net\/12821\/"},"modified":"2021-02-27T10:37:23","modified_gmt":"2021-02-27T02:37:23","slug":"ubuntu%e4%b8%8b%e7%bc%96%e8%af%91%e5%ae%89%e8%a3%85nginx%e5%8f%8a%e7%9b%b8%e5%85%b3%e8%ae%be%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/12821\/","title":{"rendered":"Ubuntu\u4e0b\u7f16\u8bd1\u5b89\u88c5Nginx\u53ca\u76f8\u5173\u8bbe\u7f6e"},"content":{"rendered":"<div class=\"idc_table\">\n<p><strong>Ubuntu\u4e0b\u7f16\u8bd1\u5b89\u88c5Nginx\u53ca\u76f8\u5173\u8bbe\u7f6e<\/strong><\/p>\n<p><strong>\u4e00\u3001\u57fa\u672c\u7684\u7f16\u8bd1\u4e0e\u5b89\u88c5<\/strong><\/p>\n<p>1\u3001\u5b89\u88c5\u4f9d\u8d56\u9879<\/p>\n<pre><span style=\"color: #0000ff\">sudo<\/span> apt-<span style=\"color: #000000\">get update\r\n<\/span><span style=\"color: #0000ff\">sudo<\/span> apt-get <span style=\"color: #0000ff\">install<\/span> build-essential zlib1g-dev libpcre3 libpcre3-dev libssl-dev libxslt1-dev libxml2-dev libgd2-xpm-dev libgeoip-dev libgoogle-perftools-dev libperl-<span style=\"color: #000000\">dev libtool\r\n<\/span><span style=\"color: #0000ff\">sudo<\/span> apt-get <span style=\"color: #0000ff\">install<\/span> openssl<\/pre>\n<p>2\u3001\u4e0b\u8f7d\u65b0\u7248\u672c\uff0c\u5230\u5b98\u7f51\u590d\u5236\u4e0b\u8f7d\u94fe\u63a5<\/p>\n<pre><span style=\"color: #0000ff\">wget<\/span> http:<span style=\"color: #008000\">\/\/<\/span><span style=\"color: #008000\">nginx.org\/download\/nginx-1.13.5.tar.gz<\/span><\/pre>\n<p>3\u3001\u89e3\u538b<\/p>\n<pre><span style=\"color: #0000ff\">tar<\/span> -zxvf nginx-<span style=\"color: #800080\">1.13<\/span>.<span style=\"color: #800080\">5<\/span>.<span style=\"color: #0000ff\">tar<\/span>.gz<\/pre>\n<p>4\u3001\u7f16\u8bd1\u5b89\u88c5<\/p>\n<pre><span style=\"color: #000000\"># \u8fdb\u5165\u89e3\u538b\u76ee\u5f55\uff1a\r\ncd nginx<\/span>-<span style=\"color: #800080\">1.13<\/span>.<span style=\"color: #800080\">5<\/span><span style=\"color: #000000\">\r\n# \u914d\u7f6e\uff1a\u8fd9\u91cc\u989d\u5916\u5b89\u88c5\u51e0\u4e2a\u6a21\u5757\r\n.<\/span>\/configure --prefix=\/usr\/local\/nginx --with-http_ssl_module --with-stream --with-mail=<span style=\"color: #000000\">dynamic\r\n\r\n# \u7f16\u8f91nginx\uff1a\r\n<\/span><span style=\"color: #0000ff\">sudo<\/span> <span style=\"color: #0000ff\">make<\/span><span style=\"color: #000000\">\r\n# \u5b89\u88c5nginx\uff1a\r\n<\/span><span style=\"color: #0000ff\">sudo<\/span> <span style=\"color: #0000ff\">make<\/span> <span style=\"color: #0000ff\">install<\/span><span style=\"color: #000000\">\r\n# \u542f\u52a8nginx\uff1a\r\n<\/span><span style=\"color: #0000ff\">sudo<\/span> \/usr\/local\/nginx\/sbin\/nginx -c \/usr\/local\/nginx\/conf\/<span style=\"color: #000000\">nginx.conf\r\n#\u6ce8\u610f\uff1a<\/span>-c \u6307\u5b9a\u914d\u7f6e\u6587\u4ef6\u7684\u8def\u5f84\uff0c\u4e0d\u52a0\u7684\u8bdd\uff0cnginx\u4f1a\u81ea\u52a8\u52a0\u8f7d\u9ed8\u8ba4\u8def\u5f84\u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u53ef\u4ee5\u901a\u8fc7 -<span style=\"color: #000000\">h\u67e5\u770b\u5e2e\u52a9\u547d\u4ee4\u3002\r\n\r\n# \u67e5\u770bnginx\u8fdb\u7a0b\uff1a\r\n<\/span><span style=\"color: #0000ff\">ps<\/span> -ef|<span style=\"color: #0000ff\">grep<\/span><span style=\"color: #000000\"> nginx\r\n\r\n# \u5efa\u7acb\u8f6f\u94fe\u63a5\uff08\u7531\u4e8e<\/span>\/usr\/local\/<span style=\"color: #000000\">bin\u5305\u542b\u4e8e$PATH\u5f53\u4e2d\uff0c\u8fd9\u6837\u5c31\u4e0d\u9700\u8981\u989d\u5916\u7684\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u4e86\uff0c\u8fd9\u610f\u5473\u7740\u53ef\u4ee5\u5728\u5176\u4ed6\u8def\u5f84\u4e0b\u76f4\u63a5\u8fd0\u884cnginx\u547d\u4ee4\uff09\r\n<\/span><span style=\"color: #0000ff\">sudo<\/span> <span style=\"color: #0000ff\">ln<\/span> -s \/opt\/dotnet\/dotnet \/usr\/local\/bin #\uff08\u521b\u5efa\u94fe\u63a5\uff09<\/pre>\n<p>&nbsp;\u7f16\u8bd1\u9009\u9879\u8bf4\u660e\uff1a<\/p>\n<p>--prefix=path \u5982\u679c\u5728\u7f16\u8bd1\u7684\u4e0d\u6307\u5b9a\u5b89\u88c5\u4f4d\u7f6e\uff0c\u90a3\u4e48\u9ed8\u8ba4\u7684\u4f4d\u7f6e\/usr\/local\/nginx\u76ee\u5f55<br \/>--sbin-path=path \u8bbe\u7f6enginx\u6267\u884c\u811a\u672c\u7684\u4f4d\u7f6e\uff0c\u8fd9\u91cc\u5982\u679c\u8bbe\u7f6e\u5728path\u53d8\u91cf\u91cc\u9762\uff0c\u5c31\u53ef\u4ee5\u5728bash\u73af\u5883\u4e0b\uff0c\u4efb\u610f\u4f7f\u7528nginx\u547d\u4ee4\uff0c\u9ed8\u8ba4\u4f4d\u7f6eprefix\/sbin\/nginx&nbsp; \u6ce8\u610f\u8fd9\u91cc\u7684prefix\u662f\u5728\u914d\u7f6e\u6587\u4ef6\u91cc\u9762\u914d\u7f6e\u7684\u8def\u5f84<br \/>--conf-path=path \u914d\u7f6enginx\u914d\u7f6e\u6587\u4ef6\u7684\u8def\u5f84\uff0c\u5982\u679c\u4e0d\u6307\u5b9a\u8fd9\u4e2a\u9009\u9879\uff0c\u90a3\u4e48\u914d\u7f6e\u6587\u4ef6\u7684\u9ed8\u8ba4\u8def\u5f84\u5c31\u4f1a\u662f prefix\/conf\/nginx.conf<br \/>--pid-path =path \u914d\u7f6enginx.pid file\u7684\u8def\u5f84\uff0c\u4e00\u822c\u6765\u8bf4\uff0c\u8fdb\u7a0b\u5728\u8fd0\u884c\u7684\u65f6\u5019\u7684\u65f6\u5019\u6709\u4e00\u4e2a\u8fdb\u7a0bid\uff0c\u8fd9\u4e2aid\u4f1a\u4fdd\u5b58\u5728pid file\u91cc\u9762\uff0c\u9ed8\u8ba4\u7684pid file\u7684\u653e\u7f6e\u4f4d\u7f6e\u662fprefix\/logs\/nginx.pid<br \/>--error-log-path=path \u8bbe\u7f6e\u9519\u8bef\u65e5\u5fd7\u7684\u5b58\u653e\u8def\u5f84\uff0c\u5982\u679c\u4e0d\u6307\u5b9a\uff0c\u5c31\u9ed8\u8ba4 prefix\/logs\/error.log<br \/>--http-log-path= path \u8bbe\u7f6ehttp\u8bbf\u95ee\u65e5\u5fd7\u7684\u8def\u5f84\uff0c\u5982\u679c\u4e0d\u6307\u5b9a\uff0c\u5c31\u9ed8\u8ba4 prefix\/logs\/access.log<br \/>--user=name \u8bbe\u7f6e\u9ed8\u8ba4\u542f\u52a8\u8fdb\u7a0b\u7684\u7528\u6237\uff0c\u5982\u679c\u4e0d\u6307\u5b9a\uff0c\u5c31\u9ed8\u8ba4 nobody<br \/>--group=name \u8bbe\u7f6e\u8fd9\u4e2a\u7528\u6237\u6240\u5728\u7684\u7528\u6237\u7ec4\uff0c\u5982\u679c\u4e0d\u6307\u5b9a\uff0c\u4f9d\u7136\u662fnobody<br \/>--with-http_ssl_module \u5f00\u542fHTTP SSL\u6a21\u5757\uff0c\u4f7fNGINX\u53ef\u4ee5\u652f\u6301HTTPS\u8bf7\u6c42\u3002\u9700\u8981\u5b89\u88c5\u4e86OPENSSL&nbsp; <br \/>--with-http_flv_module&nbsp; <br \/>--with-http_stub_status_module \u542f\u7528 \"server status\" \u9875 <br \/>--without-http_gzip_module \u7981\u7528 ngx_http_gzip_module. \u5982\u679c\u542f\u7528\uff0c\u9700\u8981 zlib <br \/>--without-http_ssi_module \u7981\u7528 ngx_http_ssi_module&nbsp; <br \/>--without-http_referer_module \u7981\u7528 ngx_http_referer_module&nbsp; <br \/>--without-http_rewrite_module \u7981\u7528 ngx_http_rewrite_module. \u5982\u679c\u542f\u7528\u9700\u8981 PCRE \u3002&nbsp; <br \/>--without-http_proxy_module \u7981\u7528 ngx_http_proxy_module&nbsp; <br \/>--without-http_fastcgi_module \u7981\u7528 ngx_http_fastcgi_module&nbsp; <br \/>--without-http_memcached_module \u7981\u7528 ngx_http_memcached_module&nbsp; <br \/>--without-http_browser_module \u7981\u7528 ngx_http_browser_module&nbsp; <br \/>--http-proxy-temp-path=PATH \u8bbe\u7f6e\u8def\u5f84\u5230the http proxy temporary files&nbsp; <br \/>--http-fastcgi-temp-path=PATH \u8bbe\u7f6e\u8def\u5f84\u5230Set path to the http fastcgi temporary files&nbsp; <br \/>--without-http \u7981\u7528 HTTP server <br \/>--with-mail \u542f\u7528 IMAP4\/POP3\/SMTP \u4ee3\u7406\u6a21\u5757&nbsp; <br \/>--with-mail_ssl_module \u542f\u7528ngx_mail_ssl_module&nbsp; <br \/>--with-openssl=DIR \u8bbe\u7f6e\u8def\u5f84\u5230OpenSSL library sources&nbsp; <br \/>--with-stream \u7528\u6765\u5b9e\u73b0\u56db\u5c42\u534f\u8bae\u7684\u8f6c\u53d1\u3001\u4ee3\u7406\u6216\u8005\u8d1f\u8f7d\u5747\u8861\u7b49<\/p>\n<p><strong>\u4e8c\u3001ssl\u7684\u76f8\u5173\u914d\u7f6e<\/strong><\/p>\n<p>1\u3001\u4f7f\u7528\u81ea\u5df1\u751f\u6210\u7684\u8bc1\u4e66\uff08\u4ec5\u4f5c\u6d4b\u8bd5\u6216\u52a0\u5bc6\u6570\u636e\u7528\uff0c\u4e0d\u88ab\u4e3b\u6d41\u6d4f\u89c8\u5668\u652f\u6301\uff09&nbsp;<\/p>\n<pre><span style=\"color: #000000\"># \u5efa\u7acb\u670d\u52a1\u5668\u79c1\u94a5\uff08\u8fc7\u7a0b\u9700\u8981\u8f93\u5165\u5bc6\u7801\uff0c\u8bf7\u8bb0\u4f4f\u8fd9\u4e2a\u5bc6\u7801\uff09\u751f\u6210RSA\u5bc6\u94a5\r\n<\/span><span style=\"color: #0000ff\">sudo<\/span> openssl genrsa -des3 -out testcert.key <span style=\"color: #800080\">1024<\/span><span style=\"color: #000000\">  \r\n# \u751f\u6210\u4e00\u4e2a\u8bc1\u4e66\u8bf7\u6c42   \r\n# \u9700\u8981\u4f9d\u6b21\u8f93\u5165\u56fd\u5bb6\uff0c\u5730\u533a\uff0c\u7ec4\u7ec7\uff0cemail\u3002\r\n# \u6700\u91cd\u8981\u7684\u662f\u6709\u4e00\u4e2acommon name\uff0c\u53ef\u4ee5\u5199\u4f60\u7684\u540d\u5b57\u6216\u8005\u57df\u540d\u3002\u5982\u679c\u4e3a\u4e86https\u7533\u8bf7\uff08\u4e0d\u4f7f\u7528\u81ea\u5df1\u751f\u6210\u7684\u8bc1\u4e66\uff0c\u5411CA\u7533\u8bf7\uff09\uff0c\u8fd9\u4e2a\u5fc5\u987b\u548c\u57df\u540d\u543b\u5408\uff0c\u5426\u5219\u4f1a\u5f15\u53d1\u6d4f\u89c8\u5668\u8b66\u62a5\u3002\u751f\u6210\u7684csr\u6587\u4ef6\u4ea4\u7ed9CA\u7b7e\u540d\u540e\u5f62\u6210\u670d\u52a1\u7aef\u81ea\u5df1\u7684\u8bc1\u4e66\r\n<\/span><span style=\"color: #0000ff\">sudo<\/span> openssl req -new -key testcert.key -<span style=\"color: #000000\">out testcert.csr\r\n# \u751f\u6210\u4e0d\u9700\u8981\u5bc6\u7801\u7684key\r\n<\/span><span style=\"color: #0000ff\">sudo<\/span> openssl rsa -<span style=\"color: #0000ff\">in<\/span> testcert.key -<span style=\"color: #000000\">out testcert_nopwd.key  \r\n# \u751f\u6210crt\u6587\u4ef6\r\n<\/span><span style=\"color: #0000ff\">sudo<\/span> openssl x509 -req -days <span style=\"color: #800080\">365<\/span> -<span style=\"color: #0000ff\">in<\/span> testcert.csr -signkey testcert_nopwd.key -out testcert.crt  <\/pre>\n<p>\u4e4b\u540e\u5c06testcert_nopwd.key\u3001testcert.csr\u3001testcert.crt\u6587\u4ef6<strong>\u590d\u5236\u5230 <span style=\"color: #3366ff\">\/etc\/ssl\/certs<\/span> \u76ee\u5f55\u4e0b<\/strong><\/p>\n<p><strong>\u7136\u540e\u914d\u7f6e <span style=\"color: #3366ff\">nginx.conf<\/span> \u6587\u4ef6<\/strong><\/p>\n<p>\u7531\u4e8e\u5728\u7f16\u8bd1\u4e2d\u8bbe\u7f6e\u7684\u8def\u5f84\u662f\/usr\/local\/nginx\uff0c\u56e0\u6b64\u8be5\u6587\u4ef6\u4f4d\u4e8e\/usr\/local\/nginx\/conf \u76ee\u5f55\u4e0b\u3002<br \/>\u6253\u5f00\u6539\u6587\u4ef6\uff0c\u5728http\u6a21\u5757\u4f5c\u5982\u4e0b\u8bbe\u7f6e\uff1a <br \/><span style=\"color: #3366ff\">ssl_certificate \/etc\/ssl\/certs\/testcert.crt;<\/span><br \/><span style=\"color: #3366ff\">ssl_certificate_key \/etc\/ssl\/certs\/testcert.key;<\/span><\/p>\n<p>&nbsp;\u5177\u4f53\u7684\u4ee3\u7801\uff1a<\/p>\n<pre><span style=\"color: #000000\">http {\r\n    include       proxy.conf;\r\n    include       mime.types;\r\n    default_type  application<\/span>\/octet-<span style=\"color: #000000\">stream;  \r\n\r\n    limit_req_zone $binary_remote_addr zone<\/span>=one:10m rate=5r\/<span style=\"color: #000000\">s;\r\n    server_tokens off;\r\n    \r\n    sendfile        on;\r\n    #tcp_nopush     on;\r\n   \r\n    keepalive_timeout  <\/span><span style=\"color: #800080\">29<\/span><span style=\"color: #000000\">;\r\n    client_body_timeout <\/span><span style=\"color: #800080\">10<\/span><span style=\"color: #000000\">; \r\n    client_header_timeout <\/span><span style=\"color: #800080\">10<\/span><span style=\"color: #000000\">; \r\n    send_timeout <\/span><span style=\"color: #800080\">10<\/span><span style=\"color: #000000\">;\r\n    #<\/span><span style=\"color: #0000ff\">gzip<\/span><span style=\"color: #000000\">  on;\r\n\r\n    # mvctest\u662f\u4e00\u4e2a\u540d\u79f0\uff0c\u4e0e\u5177\u4f53\u7684\u7a0b\u5e8f\u76f8\u5173\r\n    upstream mvctest{\r\n        server localhost:<\/span><span style=\"color: #800080\">5000<\/span><span style=\"color: #000000\">;\r\n    }\r\n\r\n    server {\r\n       \r\n        listen       <\/span><span style=\"color: #800080\">80<\/span><span style=\"color: #000000\">;\r\n        add_header Strict<\/span>-Transport-Security max-age=<span style=\"color: #800080\">15768000<\/span><span style=\"color: #000000\">;\r\n        return <\/span><span style=\"color: #800080\">301<\/span> https:<span style=\"color: #008000\">\/\/<\/span><span style=\"color: #008000\">$host$request_uri;<\/span>\r\n<span style=\"color: #000000\">    }\r\n\r\n    server {\r\n        listen <\/span>*:<span style=\"color: #800080\">443<\/span><span style=\"color: #000000\">    ssl;\r\n        server_name     localhost;\r\n        ssl_certificate <\/span>\/etc\/ssl\/certs\/<span style=\"color: #000000\">testcert.crt;\r\n        ssl_certificate_key <\/span>\/etc\/ssl\/certs\/<span style=\"color: #000000\">testcert.key;\r\n        ssl_protocols TLSv1.<\/span><span style=\"color: #800080\">1<\/span> TLSv1.<span style=\"color: #800080\">2<\/span><span style=\"color: #000000\">;\r\n        ssl_prefer_server_ciphers on;\r\n        ssl_ciphers <\/span><span style=\"color: #800000\">\"<\/span><span style=\"color: #800000\">EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH<\/span><span style=\"color: #800000\">\"<\/span><span style=\"color: #000000\">;\r\n        ssl_ecdh_curve secp384r1;\r\n        ssl_session_cache shared:SSL:10m;\r\n        ssl_session_tickets off;\r\n        ssl_stapling on; #ensure your cert is capable\r\n        ssl_stapling_verify on; #ensure your cert is capable\r\n\r\n        add_header Strict<\/span>-Transport-Security <span style=\"color: #800000\">\"<\/span><span style=\"color: #800000\">max-age=63072000; includeSubdomains; preload<\/span><span style=\"color: #800000\">\"<\/span><span style=\"color: #000000\">;\r\n        add_header X<\/span>-Frame-<span style=\"color: #000000\">Options SAMEORIGIN;\r\n        add_header X<\/span>-Content-Type-<span style=\"color: #000000\">Options nosniff;\r\n\r\n        #Redirects all traffic\r\n        location <\/span>\/<span style=\"color: #000000\"> {\r\n            proxy_pass  http:<\/span><span style=\"color: #008000\">\/\/<\/span><span style=\"color: #008000\">localhost:5000;<\/span>\r\n            limit_req   zone=one burst=<span style=\"color: #800080\">10<\/span><span style=\"color: #000000\"> nodelay;\r\n            limit_req_status <\/span><span style=\"color: #800080\">503<\/span><span style=\"color: #000000\">;\r\n        }\r\n    }\r\n\r\n}<\/span><\/pre>\n<p>\u4e0a\u9762\u5b9a\u4e49\u7684\u533a\u57df\u540d\u79f0\u4e3aone\uff08zone=one\uff09\uff0c\u5206\u914d\u5185\u5b58\u5927\u5c0f\u4e3a10m\uff08\u5982\u679c\u9650\u5236\u57df\u7684\u5b58\u50a8\u7a7a\u95f4\u8017\u5c3d\u4e86\uff0c\u5bf9\u4e8e\u540e\u7eed\u6240\u6709\u8bf7\u6c42\uff0c\u670d\u52a1\u5668\u90fd\u4f1a\u8fd4\u56de 503\uff09\uff0c<br \/>\u540c\u4e00\u4e2aip\uff08$binary_remote_addr\uff09\u5e73\u5747\u5904\u7406\u7684\u8bf7\u6c42\u9891\u7387\u4e0d\u80fd\u8d85\u8fc7\u6bcf\u79d25\u6b21\uff08rate=5r\/s\uff09\uff1b \u5982\u679c\u8d85\u8fc7\u6bcf\u79d25\u6b21\u4f46\u8d85\u8fc7\u7684\u8bf7\u6c42\u6570\u91cf\u5c0f\u4e8e\u7b49\u4e8e10\uff08burst=10\uff09\u65f6\uff0c\u4f1a\u5ef6\u8fdf\u8bf7\u6c42\u3002<br \/>\u5982\u679c\u8d85\u8fc7\u6bcf\u79d25\u6b21\u7684\u8bf7\u6c42\u6570\u8d85\u8fc710\uff0c\u5219\u7acb\u5373\u8fd4\u56de503\uff08limit_req_status 503\uff09\u7ed9\u5ba2\u6237\u7aef<\/p>\n<p>2\u3001\u7533\u8bf7\u8bc1\u4e66<br \/>\u5e38\u7528\u514d\u8d39\u8bc1\u4e66\uff1a<span style=\"color: #3366ff\">https:\/\/letsencrypt.org\/<\/span><br \/>\u901a\u5e38\u6765\u8bf4\uff0c\u5c06CSR\u6587\u4ef6\uff08\u751f\u6210\u65b9\u5f0f\u540c\u4e0a\uff0c\u6216\u8005\u6309\u7167CA\u7684\u8981\u6c42\uff09\u5c31\u662f\u4f60\u9700\u8981\u63d0\u4ea4\u7ed9 SSL \u8ba4\u8bc1\u673a\u6784\u7684\uff0c\u5f53\u4f60\u7684\u57df\u540d\u6216\u7ec4\u7ec7\u901a\u8fc7\u9a8c\u8bc1\u540e\uff0c\u8ba4\u8bc1\u673a\u6784\u5c31\u4f1a\u9881\u53d1\u7ed9\u4f60\u4e24\u4e2a\u6587\u4ef6\uff1atest_com.crt\u548ctest_com.ca-bundle<br \/>\u800ctest_com.key\u662f\u9700\u8981\u7528\u5728 Nginx \u914d\u7f6e\u91cc\u548ctest_com.crt\u3001test_com.ca-bundle\u914d\u5408\u4f7f\u7528\u7684\uff0c\u9700\u8981\u597d\u597d\u4fdd\u7ba1\u3002<\/p>\n<pre><span style=\"color: #000000\">#\u4f7f\u7528cat\u547d\u4ee4\u751f\u6210\r\n<\/span><span style=\"color: #0000ff\">cat<\/span> test_com.crt  test_com.ca-bundle &gt; test_com.pem<\/pre>\n<p>\u6700\u7ec8\u5f97\u5230\u4e24\u4e2a\u6587\u4ef6\uff1atest_com.key\u548ctest_com.pem\uff0c\u4e3a\u4e86\u7edf\u4e00\u4f4d\u7f6e\uff0c\u53ef\u4ee5\u628a\u8fd9\u4e8c\u4e2a\u6587\u4ef6\u90fd\u79fb\u52a8\u5230 <span style=\"color: #3366ff\"><strong>\/etc\/ssl\/private<\/strong><\/span> \u76ee\u5f55\u6216\u8005 <strong><span style=\"color: #3366ff\">\/etc\/ssl\/cert<\/span> <\/strong>\u76ee\u5f55\u3002 <br \/>\u7136\u540e\u53ef\u4ee5\u4fee\u6539nginx.conf\u6587\u4ef6,\u5982\u4e0b\uff1a<\/p>\n<pre><span style=\"color: #000000\">server {  \r\n    #...\r\n    ssl on;\r\n    <span style=\"color: #3366ff\">ssl_certificate <\/span><\/span><span style=\"color: #3366ff\">\/etc\/ssl\/private\/test_com.pem;\r\n    ssl_certificate_key \/etc\/ssl\/private\/<\/span><span style=\"color: #000000\"><span style=\"color: #3366ff\">test_com.key; <\/span>    #...\r\n\r\n}<\/span><\/pre>\n<p>\u540c\u4f7f\u7528\u81ea\u751f\u6210\u8bc1\u4e66\u57fa\u672c\u4e00\u6837\uff0c\u53ea\u662f\u91cd\u65b0\u66ff\u6362\u4e3a\u8ba4\u8bc1\u673a\u6784\u9881\u53d1\u7684\u8bc1\u4e66\u800c\u5df2\u3002<\/p>\n<p>3\u3001\u8fea\u83f2\uff0d\u8d6b\u5c14\u66fc\u5bc6\u94a5\u4ea4\u6362<br \/>\u4ee5\u4e0a\u8fd9\u4e48\u505a\u5e76\u4e0d\u5b89\u5168\uff0c\u9ed8\u8ba4\u662f SHA-1 \u5f62\u5f0f\uff0c\u800c\u73b0\u5728\u4e3b\u6d41\u7684\u65b9\u6848\u5e94\u8be5\u90fd\u907f\u514d SHA-1\uff0c\u4e3a\u4e86\u786e\u4fdd\u66f4\u5f3a\u7684\u5b89\u5168\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u91c7\u53d6\u8fea\u83f2\uff0d\u8d6b\u5c14\u66fc\u5bc6\u94a5\u4ea4\u6362<br \/>\u9996\u5148\uff0c\u8fdb\u5165\/etc\/ssl\/certs\u76ee\u5f55\uff0c\u6267\u884c\u547d\u4ee4 sudo&nbsp; openssl dhparam -out dhparam.pem 4096 \u751f\u6210\u4e00\u4e2adhparam.pem&nbsp;<\/p>\n<pre>test@test-VirtualBox:\/etc\/ssl\/certs$ <strong><span style=\"color: #3366ff\">sudo openssl dhparam -out dhparam.pem 4096<\/span><\/strong><span style=\"color: #000000\">\r\n[<\/span><span style=\"color: #0000ff\">sudo<\/span><span style=\"color: #000000\">] test \u7684\u5bc6\u7801\uff1a\r\nGenerating DH parameters, <\/span><span style=\"color: #800080\">4096<\/span> bit <span style=\"color: #0000ff\">long<\/span> safe prime, generator <span style=\"color: #800080\">2<\/span><span style=\"color: #000000\">\r\nThis is going to take a <\/span><span style=\"color: #0000ff\">long<\/span> <span style=\"color: #0000ff\">time<\/span><span style=\"color: #000000\">\r\n# ...\u4f1a\u7b49\u4e00\u6bb5\u65f6\u95f4\uff0c\u4e0e\u673a\u5668\u6027\u80fd\u6709\u5173...\r\n#\u5b8c\u6210\u540e\u53ef\u4ee5\u67e5\u770b\u751f\u6210\u7684\u6587\u4ef6\r\ntest@test<\/span>-VirtualBox:~$ cd \/etc\/ssl\/certs\/<span style=\"color: #000000\">\r\ntest@test<\/span>-VirtualBox:\/etc\/ssl\/<span style=\"color: #000000\">certs$ ll dhparam.pem \r\n<\/span>-rw-r--r-- <span style=\"color: #800080\">1<\/span> root root <span style=\"color: #800080\">769<\/span> 9\u6708  <span style=\"color: #800080\">27<\/span> <span style=\"color: #800080\">11<\/span>:<span style=\"color: #800080\">49<\/span><span style=\"color: #000000\"> dhparam.pem<\/span><\/pre>\n<p><strong>\u4fee\u6539 <span style=\"color: #3366ff\">nginx.conf<\/span> \u6587\u4ef6,\u6dfb\u52a0 <span style=\"color: #3366ff\">ssl_dhparam \/etc\/ssl\/certs\/dhparam.pem;<\/span> \u5373\u53ef<\/strong><\/p>\n<pre><span style=\"color: #000000\">ssl_prefer_server_ciphers on;\r\n<span style=\"color: #3366ff\"><strong>ssl_dhparam <\/strong><\/span><\/span><span style=\"color: #3366ff\"><strong>\/etc\/ssl\/certs\/<\/strong><\/span><span style=\"color: #000000\"><span style=\"color: #3366ff\"><strong>dhparam.pem; <\/strong><\/span>ssl_ciphers <\/span><span style=\"color: #800000\">\"<\/span><span style=\"color: #800000\">EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH<\/span><span style=\"color: #800000\">\"<\/span>;<\/pre>\n<p><strong>\u4e09\u3001\u542f\u52a8\u76f8\u5173<\/strong><\/p>\n<p>1\u3001\u5e38\u7528\u547d\u4ee4<\/p>\n<pre><span style=\"color: #000000\">#\u542f\u52a8 Nginx\r\n<\/span>\/usr\/local\/nginx\/sbin\/<span style=\"color: #000000\">nginx \r\n.<\/span>\/sbin\/<span style=\"color: #000000\">nginx\u3000\r\n#\u505c\u6b62 Nginx\r\n.<\/span>\/sbin\/nginx -<span style=\"color: #000000\">s stop \r\n.<\/span>\/sbin\/nginx -<span style=\"color: #000000\">s quit\r\n# <\/span>-<span style=\"color: #000000\">s\u90fd\u662f\u91c7\u7528\u5411 Nginx \u53d1\u9001\u4fe1\u53f7\u7684\u65b9\u5f0f\u3002\r\n#Nginx\u91cd\u65b0\u52a0\u8f7d\u914d\u7f6e\r\n.<\/span>\/sbin\/nginx -<span style=\"color: #000000\">s reload\r\n#\u6307\u5b9a\u914d\u7f6e\u6587\u4ef6\r\n.<\/span>\/sbin\/nginx -c \/usr\/local\/nginx\/conf\/<span style=\"color: #000000\">nginx.conf\r\n#<\/span>-<span style=\"color: #000000\">c\u8868\u793aconfiguration\uff0c\u6307\u5b9a\u914d\u7f6e\u6587\u4ef6\r\n#\u67e5\u770b Nginx \u7248\u672c\r\n.<\/span>\/sbin\/nginx -<span style=\"color: #000000\">v\r\n#\u5e2e\u52a9\r\n.<\/span>\/sbin\/nginx -<span style=\"color: #000000\">h\r\n.<\/span>\/sbin\/nginx -?<\/pre>\n<p>2\u3001\u81ea\u52a8\u542f\u52a8<br \/>\u7f16\u8bd1\u5b89\u88c5\u9700\u8981\u81ea\u5df1\u8fdb\u884c\u8bbe\u7f6e\u65b9\u53ef\u81ea\u52a8\u542f\u52a8<\/p>\n<pre># \u8bbe\u7f6enginx\u81ea\u542f\u52a8\uff0c\u5728\/lib\/systemd\/system\/<span style=\"color: #000000\"> \u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u670d\u52a1\u6587\u4ef6\r\nvim <\/span>\/lib\/systemd\/system\/nginx.service<\/pre>\n<p>\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<pre><span style=\"color: #000000\">[Unit]\r\nDescription<\/span>=nginx -<span style=\"color: #000000\"> high performance web server\r\nAfter<\/span>=network.target remote-fs.target nss-<span style=\"color: #000000\">lookup.target\r\n\r\n[Service]\r\nType<\/span>=<span style=\"color: #000000\">forking\r\nExecStart<\/span>=\/usr\/local\/nginx\/sbin\/nginx -c \/usr\/local\/nginx\/conf\/<span style=\"color: #000000\">nginx.conf\r\nExecReload<\/span>=\/usr\/local\/nginx\/sbin\/nginx -<span style=\"color: #000000\">s reload\r\nExecStop<\/span>=\/usr\/local\/nginx\/sbin\/nginx -<span style=\"color: #000000\">s stop\r\n\r\n[Install]\r\nWantedBy<\/span>=multi-user.target<\/pre>\n<p>\u6587\u4ef6\u8bf4\u660e<br \/>[Unit]\u90e8\u5206<br \/>Description:\u63cf\u8ff0\u670d\u52a1<br \/>After:\u4f9d\u8d56\uff0c\u5f53\u4f9d\u8d56\u7684\u670d\u52a1\u542f\u52a8\u4e4b\u540e\u518d\u542f\u52a8\u81ea\u5b9a\u4e49\u7684\u670d\u52a1<\/p>\n<p>[Service]\u90e8\u5206<br \/>Type=forking\u662f\u540e\u53f0\u8fd0\u884c\u7684\u5f62\u5f0f<br \/>ExecStart\u4e3a\u670d\u52a1\u7684\u5177\u4f53\u8fd0\u884c\u547d\u4ee4(\u9700\u8981\u6839\u636e\u8def\u5f84\u9002\u914d)<br \/>ExecReload\u4e3a\u91cd\u542f\u547d\u4ee4(\u9700\u8981\u6839\u636e\u8def\u5f84\u9002\u914d)<br \/>ExecStop\u4e3a\u505c\u6b62\u547d\u4ee4(\u9700\u8981\u6839\u636e\u8def\u5f84\u9002\u914d)<br \/>PrivateTmp=True\u8868\u793a\u7ed9\u670d\u52a1\u5206\u914d\u72ec\u7acb\u7684\u4e34\u65f6\u7a7a\u95f4<br \/>\u6ce8\u610f\uff1a\u542f\u52a8\u3001\u91cd\u542f\u3001\u505c\u6b62\u547d\u4ee4\u5168\u90e8\u8981\u6c42\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84<\/p>\n<p>[Install]\u90e8\u5206<br \/>\u670d\u52a1\u5b89\u88c5\u7684\u76f8\u5173\u8bbe\u7f6e\uff0c\u53ef\u8bbe\u7f6e\u4e3a\u591a\u7528\u6237<\/p>\n<pre><span style=\"color: #000000\"># \u8bbe\u7f6e\u4e86\u81ea\u542f\u52a8\u540e\uff0c\u4efb\u610f\u76ee\u5f55\u4e0b\u6267\u884c\r\nsystemctl enable nginx.service\r\n# \u542f\u52a8nginx\u670d\u52a1\r\nsystemctl start nginx.service\r\n# \u8bbe\u7f6e\u5f00\u673a\u81ea\u52a8\u542f\u52a8\r\nsystemctl enable nginx.service\r\n# \u505c\u6b62\u5f00\u673a\u81ea\u52a8\u542f\u52a8\r\nsystemctl disable nginx.service\r\n# \u67e5\u770b\u72b6\u6001\r\nsystemctl status nginx.service\r\n# \u91cd\u542f\u670d\u52a1\r\nsystemctl restart nginx.service\r\n# \u67e5\u770b\u6240\u6709\u670d\u52a1\r\nsystemctl list<\/span>-units --type=service<\/pre>\n<p><strong>\u56db\u3001\u53ef\u80fd\u9047\u5230\u7684\u95ee\u9898<\/strong><br \/>nginx: [emerg] SSL_CTX_use_PrivateKey_file(\"\/etc\/ssl\/certs\/testcert.key\") failed (SSL: error:0906406D:PEM routines:....<br \/>nginx\u542f\u52a8\u7684\u65f6\u5019\u9700\u8981\u8f93\u5165\u8bc1\u4e66\u5bc6\u7801\uff0c\u89e3\u51b3\u529e\u6cd5\u662f\u53ef\u4ee5\u4f7f\u7528\u79c1\u94a5\u6765\u751f\u6210\u89e3\u5bc6\u540e\u7684key\u6765\u4ee3\u66ff\uff0c\u6548\u679c\u662f\u4e00\u6837\u7684\uff08\u5c31\u8ddfssh\u8fde\u63a5\u5dee\u4e0d\u591a\uff09\uff0c\u8fbe\u5230\u514d\u5bc6\u7801\u91cd\u542f\u7684\u6548\u679c\uff1a<br \/>openssl rsa -in testcert.key -out untestcert.key<br \/>\u63a5\u4e0b\u6765\u4f60\u53ea\u9700\u8981\u5728nginx\u91cc\u9762\u4f7f\u7528\u65b0\u7684untestcert.key\u5373\u53ef\u4f7f\u7528https\u8fde\u63a5\u4e86\u3002<\/p>\n<p>nginx: [emerg] zero size shared memory zone \"oneip\"<br \/>\u51fa\u73b0\u6b64\u9519\u8bef\u90fd\u662f\u56e0\u4e3a\u5728\u672a\u6307\u5b9alimit_req_zone\u6307\u5b9a\u5c31\u4f7f\u7528\u4e86limit_req\u6307\u4ee4\u7684\u539f\u56e0\uff08\u6216\u8005limit_req\u4e2d\u7684name\u503c\u548climit_req_zone\u4e2d\u5b9a\u4e49\u7684name\u503c\u4e0d\u4e00\u81f4\uff09\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ubuntu\u4e0b\u7f16\u8bd1\u5b89\u88c5Nginx\u53ca\u76f8\u5173\u8bbe\u7f6e \u4e00\u3001\u57fa\u672c\u7684\u7f16\u8bd1\u4e0e\u5b89\u88c5 1\u3001\u5b89\u88c5\u4f9d\u8d56\u9879 sudo apt-get u [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28],"tags":[],"class_list":["post-12821","post","type-post","status-publish","format-standard","hentry","category-ubuntu"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/12821","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=12821"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/12821\/revisions"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=12821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=12821"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=12821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}