ubuntu安装nginx与配置

命令行安装:(当前时间为2018.11,版本为1.10.3)

sudo apt-get install nginx

安装好的文件位置:

/usr/sbin/nginx:主程序

/etc/nginx:存放配置文件

/usr/share/nginx:存放静态文件

/var/log/nginx:存放日志

查找Nginx启动文件路径

find / -name nginx.conf

查询nginx进程

ps -ef | grep nginx

重启nginx

sudo nginx -s reload

查看是否能够访问:

浏览器中填localhost

我的配置文件:

我的配置文件位置: 
/etc/nginx/conf.d/**.conf 
我的静态网页的位置: 
/var/www/** 
html中加载的js之类的文件夹和index.html在一个文件夹中

配置文件内容:

server {
  listen 80;
  server_name #你的网站IP或****.com;
  location /www1
  {
      alias /var/www/****;
      index index.html index.php index.htm;
  }
  location /www2
  {
      alias /var/www/****;
      index index.html index.php index.htm;
  }
  location  ~ .*.(jpg|jpeg|gif|png|ico|css|js|pdf|txt|webp)$
  {
      root /var/www/;
      proxy_temp_path /var/www/;
  }

配置好以后 重启nginx

sudo nginx -s reload

这时候 要访问第一个网页 就是 IP/www1 
要访问第二的网页就是 IP/www2 
关于alias和root的区别: 
root和alias是系统文件路径的设置。 
root用来设置根目录,而alias用来重置当前文件的目录。

location /img/ {
    alias /var/www/image/;
}
#若按照上述配置的话,则访问/img/目录里面的文件时,ningx会自动去/var/www/image/目录找文件
location /img/ {
    root /var/www/image;
}
#若按照这种配置的话,则访问/img/目录下的文件时,nginx会去/var/www/image/img/目录下找文件。
原文地址:https://www.cnblogs.com/flyuz/p/9962491.html