我备案了个域名,买了一个阿里云服务器,想要搭建几个本身的网站,不免要接触 nginx。html
那么我用 nginx 来干吗呢:nginx
yum install -y nginx
到官网下载,安装包,解压便可使用:官网windows
# 开机启动 systemctl enable nginx.service # 启动 systemctl start nginx.service # 使用某个配置文件启动(要先关闭 ngxin,否则会报错的) nginx -c /etc/nginx/nginx.conf # 关闭(若是这样关闭不了的话,就把 nginx 进程给杀掉) nginx -s stop # 查看 nginx 的进程 id ps -ef | grep nginx # 杀死进程(好比杀死进程1234) kill -9 1234
nginx 的默认配置文件是:/etc/nginx/nginx.conf
centos
这个配置文件会自动读取:/etc/nginx/conf.d/
文件夹下的全部 .conf
文件。服务器
假如你写了个网站,须要用到 nginx ,那么你就把配置文件丢到 /etc/nginx/conf.d/
文件夹下,而后重启 nginx 就好了:网站
nginx -s stop nginx -c /etc/nginx/nginx.conf
假设咱们有两个网站:www.example.com
blog.example.com
阿里云
它们分别有两个 ngxin 配置文件放在:/home/www.example.com/www.example.com.nginx.conf
/home/blog.example.com/blog.example.com.nginx.conf
url
服务器对外开放 80 端口,两个网站对应的程序端口分别为:centos7
www.example.com 程序 8000 www.example.com 静态资源 8080 blog.example.com 程序 8001 blog.example.com 静态资源 8081
那么他们的配置内容分别为:代理
# /home/www.example.com/www.example.com.nginx.conf server { # 服务器对外只监听 80 端口 listen 80; # 当 80 端口监听到了来自 www.example.com 的请求 server_name www.example.com; # 那么把这个请求转到 http://localhost:8080 location / { proxy_pass http://localhost:8080; } } server { # 监听到 8080 端口有人发起请求(其实就是上面转过来的↑,用户不能直接访问 8080 端口的) listen 8080; # 只容许本机访问 server_name localhost; # 程序根目录 root /home/www.example.com; # 默认的网页文件 index index.html index.htm; # 匹配全部 uri (若是 url 为:http://www.example.com/hehe,那么 uri 为:/hehe) location / { # 咱们的程序其实是在 8000 端口上 proxy_pass http://localhost:8000$request_uri; # 下面这一大堆,欲知详情本身查 proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; proxy_max_temp_file_size 0; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; } # 匹配首页 location = / { proxy_pass http://localhost:8000; } # 匹配 /**.** 结尾的 uri,而且不是 /**.html、/**.htm location ~* ^.+\/[^\/]+(?=\.)([^\/](?!(html|htm)))+$ { # 匹配到的都定位到静态资源目录里 root /home/www.example.com/static; etag on; expires max; } }
# /home/blog.example.com/blog.example.com.nginx.conf server { # 服务器对外只监听 80 端口 listen 80; # 当 80 端口监听到了来自 blog.example.com 的请求 server_name blog.example.com; # 那么把这个请求转到 http://localhost:8081 location / { proxy_pass http://localhost:8081; } } server { # 监听到 8081 端口有人发起请求(其实就是上面转过来的↑,用户不能直接访问 8081 端口的) listen 8081; # 只容许本机访问 server_name localhost; # 程序根目录 root /home/blog.example.com; # 默认的网页文件 index index.html index.htm; # 匹配全部 uri (若是 url 为:http://blog.example.com/hehe,那么 uri 为:/hehe) location / { # 咱们的程序其实是在 8001 端口上 proxy_pass http://localhost:8001$request_uri; # 下面这一大堆,欲知详情本身查 proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; proxy_max_temp_file_size 0; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; } # 匹配首页 location = / { proxy_pass http://localhost:8001; } # 匹配 /**.** 结尾的 uri,而且不是 /**.html、/**.htm location ~* ^.+\/[^\/]+(?=\.)([^\/](?!(html|htm)))+$ { # 匹配到的都定位到静态资源目录里 root /home/blog.example.com/static; etag on; expires max; } }
注意看两个配置的区别。
假如咱们每一个网站程序放在一个文件夹里,该程序的 nginx 配置文件也应该放在这个文件夹里才方便管理。但前面提到,咱们须要把配置文件丢到 /etc/nginx/conf.d/
文件夹下,怎样才能使这个配置文件既在程序文件夹下,又在 /etc/nginx/conf.d/
文件夹下呢?
假如咱们在程序文件夹下有一个 ngxin 配置文件:/home/www.example.com/www.example.com.nginx.conf
咱们须要给这个文件建立一个软连接到 /etc/nginx/conf.d/
下:
ln -s /home/example/www.example.com.nginx.conf /etc/nginx/conf.d/www.example.com.nginx.conf
这样操做以后,当咱们改程序文件夹下的配置文件,/etc/nginx/conf.d/
下与之对应的配置文件也会被修改,修改后重启 nginx 就可以使新的 ngxin 配置生效了。