Nginx 指定域名(或子域名)和网站绑定
<h1>问题起因</h1><p>博主最近在 CentOS 上面部署另外一个网站,但并不想通过端口号来访问,因为端口号对于 SEO 优化不利,且用户访问较繁琐(使用域名不就是为了方便用户访问吗?再引入端口号岂不是和使用域名的目的相悖吗?),因此想在 CentOS 的 80 端口上同时运行两个网站,nginx 通过请求的域名来返回相应的根目录下的网站,达到 80 端口复用,同时运行多个网站的目的。</p>
<h1>实现步骤</h1>
<p>为方便您检查路径,本文在需要对路径进行要求时,专门使用 <code>pwd</code> 命令打印出博主当前步骤所在路径,便于您检查。</p>
<h2>检查 nginx 配置文件路径(重要)</h2>
<p>注意:这一步非常关键,如果修改了错误的 nginx 配置文件,将导致所有修改均无效,甚至会让您在探索了数个小时后仍无法得知修改无效的原因是什么。<br>使用命令</p>
<pre class="prettyprint"><code class="prism language-bash has-numbering">$ nginx -V
</code></pre>
<p>来查看 nginx 的一些配置信息,如下(您的和博主的显示内容可能不同,不影响):</p>
<pre class="prettyprint"><code class="prism language-text has-numbering">nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
built with OpenSSL 1.0.2k-fips26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_auth_request_module --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'</code></pre>
<p>其中只需要关注到 <code>--conf-path=/etc/nginx/nginx.conf</code> 这个条目,这个条目指明了当前 nginx 的程序使用的默认配置文件。</p>
<h2>新建一个站点配置文件</h2>
<p>根据上面的配置文件路径,首先切换目录到 nginx 的配置路径下:</p>
<pre class="prettyprint"><code class="prism language-bash has-numbering">$ <span class="token function">cd /etc/nginx
</span></code></pre>
<p>然后新建一个站点配置文件,建议放置在 nginx 配置目录下的 <code>vhost</code> 目录内,如果您当前没有此目录,可新建一个:</p>
<pre class="prettyprint"><code class="prism language-bash has-numbering">$ <span class="token function">pwd
/etc/nginx
$ <span class="token function">sudo <span class="token function">mkdir vhost
$ <span class="token function">ls
conf.d fastcgi_params mime.types scgi_params vhost
default.d fastcgi_params.defaultmime.types.defaultscgi_params.default win-utf
fastcgi.conf koi-utf nginx.conf uwsgi_params
fastcgi.conf.defaultkoi-win nginx.conf.defaultuwsgi_params.default
</span></span></span></span></code></pre>
<p>注意:<code>/etc</code> 是系统目录,一般用户不具有修改权限,需要使用 <code>sudo</code> 以管理员权限在该目录中进行修改,如创建、修改文件,创建文件夹等操作。</p>
<p>进入刚刚创建的 <code>vhost</code> 目录,新建一个配置文件,例如 <code>mysite.conf</code>,该配置文件文件名无限制,最好是你的网站名称之类的,便于辨认,但一定是以 <code>.conf</code> 为后缀。</p>
<pre class="prettyprint"><code class="prism language-bash has-numbering">$ <span class="token function">pwd
/etc/nginx
$ <span class="token function">cd vhost/
$ <span class="token function">pwd
/etc/nginx/vhost
$ <span class="token function">sudo <span class="token function">touch mysite.conf
$ <span class="token function">ls
mysite.conftrans.conf</span></span></span></span></span></span></code></pre>
<p>其中,<code>touch</code> 命令创建了一个名为 <code>mysite.conf</code> 的配置文件(空文件),是本文中要介绍的配置文件,另一个是博主正在运行的网站,无需在意。</p>
<h2>修改网站配置文件信息</h2>
<p>本文中将建立一个名为 <code>mysite</code> 的网站,网站根目录为 <code>/home/www/mysite</code>,网站绑定域名 <code>mysite.jinhangdev.cn</code>。</p>
<p>使用任意文字编辑器打开 <code>mysite.conf</code>,输入如下内容并保存(注意需管理员权限):</p>
<pre class="prettyprint"><code class="has-numbering">server {
listen 80; # 网站的端口一般为 80,可以与其他网站一起使用该端口
server_namemysite.jinhangdev.cn; # 要绑定的域名(或子域名)
root /home/www/mysite; # 该网站的根目录
location / { # 不用管
}
}</code></pre>
<p>以上内容为一个 <code>server</code> 的内容,下面讲 <code>nginx.conf</code> 配置的时候会再区分把一行配置写在 <code>server</code> 或写在 <code>server</code> 外的区别。</p>
<pre class="prettyprint"><code class="prism language-bash has-numbering">$ <span class="token function">pwd
/etc/nginx/vhost
$ <span class="token function">ls
mysite.conftrans.conf
$ <span class="token function">sudo vim mysite.conf
$ <span class="token function">cat mysite.conf
server <span class="token punctuation">{
listen 80<span class="token punctuation">;
server_name mysite.jinhangdev.cn<span class="token punctuation">;
root /home/www/mysite<span class="token punctuation">;
location /<span class="token punctuation">{
<span class="token punctuation">}
<span class="token punctuation">}</span></span></span></span></span></span></span></span></span></span></span></code></pre>
<p>上面我们已经新建好了一个网站,该网站使用服务器的 80 端口,绑定的域名为 <code>mysite.jinhangdev.cn</code>,网站根目录使用 <code>/home/www/mysite</code>。</p>
<h2>修改 nginx 配置文件</h2>
<p>下面对关键文件 <code>nginx.conf</code> 进行配置,建议在修改前先备份,养成好的习惯:</p>
<pre class="prettyprint"><code class="prism language-bash has-numbering">$ <span class="token function">pwd
/etc/nginx
$ <span class="token function">sudo <span class="token function">cp nginx.conf nginx.conf.bak</span></span></span></code></pre>
<p>将原有配置存储到 <code>nginx.conf.bak</code> 后,继续下面操作,若后面发现修改出错,可将 <code>nginx.conf.bak</code> 改名回 <code>nginx.conf</code> 使用。</p>
<p>以超级用户权限使用文本编辑器打开 <code>nginx.conf</code>:</p>
<pre class="prettyprint"><code class="prism language-bash has-numbering">$ <span class="token function">sudo vim nginx.conf</span></code></pre>
<p>并进行编辑。</p>
<h2>Nginx 配置文件的结构</h2>
<p>我们这里只介绍和配置域名绑定相关的部分结构,即 <code>http</code> 小节:</p>
<pre class="prettyprint"><code class="has-numbering">http {
(各种配置)
}</code></pre>
<p>设置在此处的配置都是 <code>http</code> 的全局配置,如果要对某个网站单独配置,则需要对每个 <code>server</code> 分别设置:</p>
<pre class="prettyprint"><code class="has-numbering">http {
(此处的配置是全局的配置参数)
server {
(此处的配置是本 server 的配置参数)
}
server {
(此处的配置是本 server 的配置参数)
}
(此处的配置是全局的配置参数)
}</code></pre>
<p>但是我们并不提倡把所有 <code>server</code> 都展开写在 <code>nginx.conf</code> 中,因此我们使用一句 <code>include</code> 语句,把 <code>vhost</code> 下所有网站的配置都引用进来。注意:<code>include</code> 命令只是简单地进行文本替换。</p>
<p>于是我们在 <code>nginx.conf</code> 的 <code>http</code> 节内部的末尾写上一句:</p>
<pre class="prettyprint"><code class="has-numbering">http {
(前面的若干配置)
include /etc/nginx/vhost/*.conf;
}</code></pre>
<p>这样一来,<code>vhost</code> 下的所有 <code>.conf</code> 文件均被文本替换式地引入到 <code>nginx.conf</code> 里面了。</p>
<h2>重启 nginx 服务</h2>
<p>使用命令:</p>
<pre class="prettyprint"><code class="prism language-bash has-numbering">$ <span class="token function">sudo <span class="token function">service nginx restart
Redirecting to /bin/systemctl restart nginx.service</span></span></code></pre>
<p>重启 nginx 服务。</p>
<h3>出现问题:重启服务报错</h3>
<p>此时可使用命令:</p>
<pre class="prettyprint"><code class="prism language-bash has-numbering">$ <span class="token function">sudo <span class="token function">service nginx status -l</span></span></code></pre>
<p>来查看报错信息和错误日志。输入此命令后,日志不会马上显示出来,稍等几秒会显示出来,<code>-l</code> 选项使得每条日志能够完整显示在屏幕上,否则当一条日志过长时,中间的文本将被 <code>...</code> 替代。<br>若出现问题,十有八九日志都会说是载入 <code>nginx.conf</code> 出错,这多半是因为您在 <code>nginx.conf</code> 或 <code>mysite.conf</code> 中有错误的配置。</p>
<h3>访问新网站</h3>
<p>为新的二级域名添加 DNS 解析后,在浏览器中访问 <code>mysite.jinhangdev.cn</code>,正常情况下会报 <code>404 Not found</code> 的错误,这是因为本文到目前为止还没有去创建这个新网站的根目录,出现 <code>404</code> 错误反而说明前面的配置是正确的,所以下一步我们将创建一个简单的网页运行起来。</p>
<h2>建立新网站的根目录(或使用已有目录)</h2>
<p>博主习惯将网站的文件放在 <code>/home/www</code> 下,并给该目录 <code>0777</code> 权限,以免出现 <code>403</code> 错误,虽然这样不太安全。鉴于博主水平有限,希望有安全需求的读者关注其他安全大牛的文章,博主深表歉意。</p>
<p>在 <code>/home/www</code> 下建立目录 <code>mysite</code>(<code>/home/www</code> 目录已经被博主创建好,并使用 <code>chmod</code> 命令赋予其 <code>0777</code> 权限):</p>
<pre class="prettyprint"><code class="prism language-bash has-numbering">$ <span class="token function">cd /home/www/
$ <span class="token function">mkdir mysite
$ <span class="token function">ls
mysitetrans</span></span></span></code></pre>
<p>切换到 <code>mysite</code> 目录下,简单创建一个网页:</p>
<pre class="prettyprint"><code class="prism language-bash has-numbering">$ <span class="token function">pwd
/home/www
$ <span class="token function">cd mysite/
$ <span class="token keyword">echo hello<span class="token operator">! <span class="token operator">> index.html
$ <span class="token function">ls
index.html
$ <span class="token function">cat index.html
hello<span class="token operator">!</span></span></span></span></span></span></span></span></code></pre>
<p>此时在浏览器中重新访问,即可看到一行 <code>hello!</code> 了,表示新网站创建成功。<br><img src="https://img-blog.csdnimg.cn/20181218180202493.JPG"></p>
<h1>结语</h1>
<p>以上便是一个新网站的域名绑定的创建过程,若有您不明白或博主的错误之处,欢迎在评论区指正,感激不尽!</p><br><br>
来源:https://www.cnblogs.com/missbye/p/13716648.html
頁:
[1]