nginx服务的搭建

1.搭建Http静态服务器环境


(1)首先用ssh登陆自己的云服务器,我的服务器为CentOS.
ssh root@123.207.35.113
(2)安装nginx
yum install nginx -y
(3)安装完成后,启动服务
nginx
(4)此时,访问自己的公网IP,如`http://123.207.35.113` 可以看到 Nginx 的测试页面 

2.配置静态服务器访问路径

外网用户访问服务器的 Web 服务由 Nginx 提供,Nginx 需要配置静态资源的路径信息才能通过 url 正确访问到服务器上的静态资源。

(1)打开 Nginx 的默认配置文件 /etc/nginx/nginx.conf ,修改 Nginx 配置,将默认的 root /usr/share/nginx/html; 修改为: root /data/www;,配置文件将 /data/www/static 作为所有静态资源请求的根路径,如访问: http://123.207.35.113/static/index.js,将会去 /data/www/static/ 目录下去查找 index.js。

#进入配置文件的目录
cd /etc/nginx
#查看该目录下的文件
ll
#编辑配置文件
vim nginx.conf
#原配置文件
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /data/www;

        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

}

(2)重新启动nginx服务

nginx -s reload

(3)重启后,现在我们应该已经可以使用我们的静态服务器了,现在让我们新建一个静态文件,查看服务是否运行正常。
首先让我们在 /data 目录 下创建 www 目录,如:

#创建目录
mkdir -p /data/www
#进入目录
cd /data/www
#编辑文件
vim index.html
#文件内容
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>第一个静态文件</title>
</head>
<body>
Hello world!
</body>
</html>

(4)现在访问 http://123.207.35.113/index.html 应该可以看到页面输出 Hello world! 一个基于 Nginx 的静态服务器就搭建完成了,现在所有放在 /data/www 目录下的的静态资源都可以直接通过域名访问。

server{

        listen 80;
        server_name [hostname];
        location / {
                root /var/www;
                index index.html index.htm index.php;
        }
	location /api/ {
		proxy_pass https://host/web/;
	 }

    	location /upload/ {
		proxy_pass http://host;
   	 }

    	location /web/ {
		proxy_pass http://testhost/web/;
   	 }
	location ^~ /static/ {
		alias /var/www/static/;
   	 }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值