跳转至

使用Nginx配置测试站点

约 224 个字 73 行代码 预计阅读时间 2 分钟

一、相关参数

  • 操作系统:Rocky Linux 9.3
  • Nginx:1.20.1

二、创建测试站点目录

执行命令:

sudo mkdir -p /var/www/test.fotianmoyin.com
执行示例:
[fotianmoyin@fotianmoyin-ecs ~]$ sudo mkdir -p /var/www/test.fotianmoyin.com
[sudo] password for fotianmoyin: 
[fotianmoyin@fotianmoyin-ecs ~]$ ll /var/www
total 0
drwxr-xr-x 2 root root 6 Feb 24 16:05 test.fotianmoyin.com
赋予站点目录访问权限,执行命令:
sudo chown -R $USER:$USER /var/www/test.fotianmoyin.com/html
执行示例:
[fotianmoyin@fotianmoyin-ecs ~]$ sudo chown -R $USER:$USER /var/www/test.fotianmoyin.com/html
[fotianmoyin@fotianmoyin-ecs ~]$ ll /var/www/test.fotianmoyin.com
total 0
drwxr-xr-x 2 fotianmoyin fotianmoyin 6 Feb 24 16:07 html

三、创建测试页面

使用vim创建测试首页index.html:

vim /var/www/test.fotianmoyin.com/html/index.html
内容如下:
<html>
    <head>
        <title>Welcome to test.fotianmoyin.com</title>
    </head>
    <body>
        <h1>Success! Your Nginx server is successfully configured for <em>test.fotianmoyin.com</em>. </h1>
        <p>This is a sample page.</p>
    </body>
</html>

四、创建Nginx服务器块

编辑/etc/nginx/conf.d/test.fotianmoyin.conf文件:

sudo vim /etc/nginx/conf.d/test.fotianmoyin.conf
内容如下:
server {
        listen 80;
        listen [::]:80;

        root /var/www/test.fotianmoyin.com/html;
        index index.html index.htm index.nginx-debian.html;

        server_name test.fotianmoyin.com;

        location / {
                try_files $uri $uri/ =404;
        }
}
可以使用如下命令,验证Nginx配置是否有问题:
sudo nginx -t
执行示例如下:
[fotianmoyin@fotianmoyin-ecs ~]$ sudo nginx -t
[sudo] password for fotianmoyin: 
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
执行如下命令,重新加载nginx配置:
sudo systemctl reload nginx
设置SELinux安全上下文,允许Nginx提供/var/www/test.fotianmoyin.com/内容服务,执行如下命令:
chcon -vR system_u:object_r:httpd_sys_content_t:s0 /var/www/test.fotianmoyin.com/
执行示例如下:
[fotianmoyin@fotianmoyin-ecs ~]$ chcon -vR system_u:object_r:httpd_sys_content_t:s0 /var/www/test.fotianmoyin.com/
changing security context of '/var/www/test.fotianmoyin.com/html/index.html'
changing security context of '/var/www/test.fotianmoyin.com/html'
changing security context of '/var/www/test.fotianmoyin.com/'

五、访问测试

访问http://test.fotianmoyin.com浏览页面如下: test.fotianmoyin.com

六、添加ssl支持

编辑/etc/nginx/conf.d/test.fotianmoyin.conf文件:

sudo vim /etc/nginx/conf.d/test.fotianmoyin.conf
修改内容如下:
server {
        listen 80;
        listen [::]:80;

        listen 443 ssl http2;
        listen [::]:443 ssl http2;
        ssl_certificate /home/fotianmoyin/.acme.sh/fotianmoyin.com_ecc/fullchain.cer;
        ssl_certificate_key /home/fotianmoyin/.acme.sh/fotianmoyin.com_ecc/fotianmoyin.com.key;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers on;

        root /var/www/test.fotianmoyin.com/html;
        index index.html index.htm index.nginx-debian.html;

        server_name test.fotianmoyin.com;

        location / {
                try_files $uri $uri/ =404;
        }
}
验证Nginx配置是否有问题:
sudo nginx -t
执行示例如下:
[fotianmoyin@fotianmoyin-ecs ~]$ sudo nginx -t
[sudo] password for fotianmoyin: 
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
访问https://test.fotianmoyin.com浏览页面如下: test.fotianmoyin.com_ssl