Nginx server配置

项目一般都需要前后端的配置,用二级域名把它区分开:
首先在nginx.conf;里面加一句话:
http{
  #这里面有很多其他的配置 如:gzip FastCGI等等
include vhosts/*.host;
}

文件夹自己定义  反正引进去就好了:

cd yourdirname 创建一个文件 ,命名为:default.host,写入下面的代码

server {
  listen 80 default;
  rewrite ^(.*) http://www.xuesong0323.cn permanent;
}

这里创建server的时候  在监听80端口的后面加了一个default,目的是为了一个默认重定向

每一个二级域名添加进去的时候都推荐新建一个文件,这样方便以后管理。下面举个栗子:

文件名:www.host

server {
  listen  80;
  server_name www.xuesong0323.cn;
  
  error_page 404 = http://www.xuesong0323.cn/;
  
  location ~ ^/www/.*.html$ {
         proxy_redirect off;
    proxy_set_header Host pms.dohohome.com;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_pass http://127.0.0.1:80;
  }
  
  location / {
    root   your root dirname;  #你的项目入口绝对路径
    index  index.html index.php index.htm;#入口可以解析的文件类型 如果有需要,可以吧index.html/php/htm改成自己需要的文件名和类型
  }
  
  include rewrite/www.conf; #这里我是引入了www的路由重写规则,具体需要看自己的项目需求  
  
  location ~*.php$ {
    limit_conn   connlimitzone 15 ;
    limit_req zone=rqlimitzone burst=5 nodelay;
    root           /apps/web/doho/shop_fend/public/; #项目入口绝对路径
    #try_files $uri=404;
    fastcgi_pass   127.0.0.1:9000;  #php-fpm要用的 不用管
    fastcgi_index  index.php;     #fastcgi_index参数,具体解释见上一篇
    fastcgi_param  SCRIPT_FILENAME  your root dirname$fastcgi_script_name;
    include        fastcgi_params;
  }

 location ~* .(htaccess)$ {rewrite ^/(.*) /index.php last;}

  access_log  /var/log/nginx/www.access.log  main;  #日志记录
}

还有一个配置其他端口的:(其实都一样的)8081.host

server {
        listen       8081;
        server_name  www.xuesong0323.cn;
        #access_log  logs/host.access.log  main;
        location / {
            root   /home/***/;#向配哪里配哪里,后面一致就好了
            index  index.html index.php 1.php 1.html;
        }

        error_page   500 502 503 504  /50x.html;#错误页面
        location = /50x.html {
       #错误页面路径 root /home/***/
;#这个一般不会改,除非你觉得自己写的的比较好看= =,我这个是改过的 } location ~ .php$ { root /home/***/; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /$document_root$fastcgi_script_name; include fastcgi_params; } }
原文地址:https://www.cnblogs.com/pfdltutu/p/9150351.html