乡野农夫 發表於 2020-4-23 14:16:00

nginx绑定域名(https)

<p>#查看启动情况</p>
<p>ps -ef|grep nginx</p>
<p>#查看是否启动成功</p>
<p>curl 192.168.0.177</p>
<p>#查看端口情况</p>
<p>netstat -ano|grep 80</p>
<p>修改nginx配置(我的nginx是安装在/lnmp/nginx上的):</p>
<p>vim /lnmp/nginx/conf/nginx.conf</p>
<p><img src="https://img2020.cnblogs.com/blog/1776077/202004/1776077-20200423140726572-1494126099.png" alt=""></p>
<p>&nbsp;在/lnmp/nginx/vhost/ 中新建test.conf</p>
<p>vim test.conf</p>
<p>配置如下:</p>
<div class="cnblogs_Highlighter">
<pre class="brush:php;gutter:true;">#测试
server {
    # 监听端口
    listen       8989;
    # 绑定域名
    server_name120.53.29.187;
    # 编码
    charset utf-8;
    # 网站根目录,可自定义
    root   /www/web;
    # 主机默认文档
    index index.html index.php;
    # 成功与错误日志
    access_log/www/weblogs/test.access.log main gzip=4 flush=5m;
    error_log   /www/weblogs/test.error.log;<br>
    # URL重写(根据实际需求填写,以下是默认跳转index.php)
    location / {
      if (!-e $request_filename) {
            rewrite ^/index.php(.*)$ /index.php?s=$1 last;
            rewrite ^/(.*)$ /index.php?s=$1 last;
            break;
      }
    }

    # nginx 和 php 关联
    # 配置FastCGI,PHP 脚本请求全部转发到 FastCGI处理
    location ~ .*\.php$ {
      # 设置监听端口(多版本php用端口号区分)
      fastcgi_pass   127.0.0.1:9000;
      # 设置脚本文件请求的路径
      fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;
      # 引入fastcgi的配置文件
      include      fastcgi_params;
    }

}  </pre>
</div>
<p>若需要配置https,则配置如下</p>
<div class="cnblogs_Highlighter">
<div class="cnblogs_Highlighter">
<pre class="brush:php;gutter:true;"># test
server {
    listen 80;
    server_name www.test.com;
    location / {
      # 301 跳转到https带上url参数$request_uri;
      return 301 https://$server_name$request_uri;
    }
}
server {
    # 监听443端口
    listen       443;
    # 绑定域名
    server_namewww.test.com;
    # 编码
    charset utf-8;
    # 网站根目录
    root   /www/web;
    # 主机默认文档
    indexindex.html index.php;
    # 成功与错误日志
    access_log/www/weblogs/www.test.com.access.log;
    error_log   /www/weblogs/www.test.com.error.log;
    # 开启ssl
    ssl on;
    # ssl证书存放路径
    ssl_certificate /www/ssl/2019/2906479_www.test.com.pem;
    # ssl证书存放路径
    ssl_certificate_key /www/ssl/2019/2906479_www.test.com.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    # 按照这个协议配置
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    # 按照这个配置
    ssl_prefer_server_ciphers on;
    # URL重写(根据实际需求填写)
    location / {
      if (!-e $request_filename) {
            rewrite ^(.*)$ /index.php?s=/$1 last;
      }
      try_files $uri $uri/ /index.php?$args;
    }
    # nginx 和 php 关联
    # 配置FastCGI,PHP 脚本请求全部转发到 FastCGI处理
    location ~ .*\.php$ {
      # 设置监听端口(多版本php用端口号区分)
      fastcgi_pass   127.0.0.1:9000;
      # 设置脚本文件请求的路径
      fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;
      # 引入fastcgi的配置文件
      include      fastcgi_params;
    }
}</pre>
</div>
</div>
<p>保存之后,查看配置是否出错</p>
<p>nginx -t</p>
<p>重启nginx</p>
<p>service nginx restart</p>
<p>&nbsp;</p>

</div>
<div id="MySignature" role="contentinfo">
    <div>
<p style="padding-top: 5px; padding-right: 10px; padding-bottom: 10px; font-family: 微软雅黑; font-size: 14px; height: 150px" id="PSignature">
            ♥ 作者:<span style="font-weight: bold; font-size: large">离岸少年</span>
            <br>
            ♠ 出处:https://www.cnblogs.com/jackzhuo/
            <br>
            ♣ 本博客大多为学习笔记或读书笔记,本文如对您有帮助,还请多推荐下此文,如有错误欢迎指正,相互学习,共同进步。
      </p>
    </div><br><br>
来源:https://www.cnblogs.com/jackzhuo/p/12760497.html
頁: [1]
查看完整版本: nginx绑定域名(https)