【服务器】nginx二级域名配置 - Go语言中文社区

【服务器】nginx二级域名配置


关于Nginx二级域名的配置,网上有好多文章和教程(好多都一样的),然而发现有太多复制粘贴且不适用的,这里安利一个炒鸡简单的方式配置nginx二级域名的方法。

首先介绍使用到的服务器的环境LNMP环境(其中nginx版本1.4.6,服务器是ubuntu 14.04的),可能因为版本问题有配置的方式和文件的位置有细微不同。

服务商域名配置

我们先去域名提供商哪里去将我们的域名和我们的主机(公网IP)匹配上


开始配置顶级域名


这个配完直接访问域名验证是否可以正常访问到我们的主机(这里配置完成通常在1分钟左右生效),完成之后开始配置二级域名



同样的道理,这里配置完,验证一下能否正常访问


Nginx服务器配置

域名提供商上配置的信息相当在DNS服务器上给我们配置了,这个配置后只能将我们的主机和域名匹配上,我们还需要在服务器上配置来将我们的项目配置到二级域名上,比如说我们服务器上有个博客项目叫blog,我们的域名是qq.com,我想通过blog.qq.com访问到我们的blog项目,那么我们需要把我们这个想法告诉服务器,通过服务器的配置文件

nginx的目录在/etc/nginx   在/etc/nginx目录下nginx.conf是主要配置文件,里面有这样的描述
http{
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
包含了指定目录下的配置文件,所以我们去看一下这两个文件夹,其中/etc/nginx/conf.d目录里是空的, /etc/nginx/sites-enabled目录里有个叫default的文件
打开这个文件(下面是整个文件的描述,自定义的server配置我会标红,如果不想细看,可以直接往下拉)

# You may add here your
# server {
# ...
# }
# statements for each of your virtual hosts to this file
# '#'在linux中通常表示注释当行,这里面我们发现Google的Nginx还是挺贴心的,给出了解释和参考文档地址,如果我们的服务器有https访问得需求,也可以在这个文件中配置
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##


server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;


root /usr/share/nginx/html;
index index.php index.html index.htm;


# Make site accessible from http://localhost/
server_name localhost;


location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}


# Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
#location /RequestDenied {
# proxy_pass http://127.0.0.1:8080;    
#}


#error_page 404 /404.html;


# redirect server error pages to the static page /50x.html
#
#error_page 500 502 503 504 /50x.html;
#location = /50x.html {
# root /usr/share/nginx/html;
#}


# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
# fastcgi_split_path_info ^(.+\.php)(/.+)$;
# # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#
# With php5-cgi alone:
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
# fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
#include fastcgi_params;
}



# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}


#MY CONFIG START
server
{
listen   80;
server_name  qq.com;
  root   /usr/share/nginx/html;
    index  index.php index.html index.htm;
    location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}
}
#先配置的是qq.com,相信我有配置这个的必要性,location里面的是为了让我们的Nginx服务器知道.php文件要去找php解释器执行,不配置可能会出现访问即下载文件的现象
#不要问我为什么,我是抄上面那个默认的server的,错不了,手动滑稽
server
{
    listen   80;
    server_name   blog.qq.com;
    root   /usr/share/nginx/html/blog;
    index  index.php index.html index.htm;
    location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}
}
#MY CONFIG END


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# root html;
# index index.html index.htm;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}




# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
#
# root html;
# index index.html index.htm;
#
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
#
# ssl_session_timeout 5m;
#
# ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
# ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
# ssl_prefer_server_ciphers on;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}
版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/xiaoping0915/article/details/53899465
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2023-01-02 15:53:08
  • 阅读 ( 287 )
  • 分类:Go Web框架

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢