最近在学习go web开发,需要搭一个能在线上访问的环境,刚好之前在digitalocean上租了一台云主机,并且已经在上面搭了一个WordPress博客系统,现在手里也有两个域名,于是打算在那台云主机上配置两个不同域名的web服务器。由于我用的是nginx作为代理,因此在这里只需要修改nginx配置文件,即可实现按照域名不同将请求转发给不同的web服务器。
WordPress用的是PHP语言,因此nginx需要将请求转发给php-fpm监听的端口上,beego用的是go语言,因此nginx需要将请求转发给beego所启动的web服务器的监听端口上。
下面看nginx的具体配置:
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
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;
client_max_body_size 5M;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
# 配置WordPress
server {
# nginx监听在80端口
listen 80;
listen [::]:80;
# 网站域名是abc.com
server_name abc.com;
# 配置网站的根目录
root /var/www/html;
index index.html index.htm index.php;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
location ~ .php$ {
# 配置请求转发端口,php-fpm监听在9000端口
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# 配置go web
server {
# nginx监听在80端口
listen 80;
# 网站域名是xyz.com
server_name xyz.com;
charset utf-8;
# 配置网站访问日志文件
access_log /root/log/xyz.com.access.log;
# 配置网站根目录
root /root/go/src/xyz/;
location /(css|js|fonts|img)/ {
access_log off;
expires 1d;
try_files $uri @backend;
}
location / {
try_files /_not_exists_ @backend;
}
location @backend {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
# 配置请求转发端口,go web监听在8080端口
proxy_pass http://127.0.0.1:8080;
}
}
}