ubuntu下nginx安装、基本配置及经常使用命令

1 安装:html

sudo apt-get install nginx

2 启动服务:
java

sudo service nginx start

或者linux

sudo /etc/init.d/nginx start

nginx默认设置了80端口的转发,启动后能够在浏览器访问http://localhost  检查是否启动成功。
nginx


3 配置ubuntu

默认配置文件:/etc/nginx/nginx.conf浏览器

该配置文件中有两行,是用来加载外部的配置文件,以下:cookie

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;

其中第二行的 /etc/nginx/sites-enabled/ 下有一个 default 文件,nginx的默认代理配置就在这里面。内容以下图:spa

精简后以下:代理

server {
	listen 80 default_server;
	listen [::]:80 default_server;	
	root /var/www/html;	
	index index.html index.htm index.nginx-debian.html;
	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;
	}
}

在 /etc/nginx/conf.d/ 路径下新建一个本身项目的nginx配置文件:myproject.conf, 名字能够随便取,而后将精简后的这部分代码复制过来,根据本身的项目作相应配置,以下:rest

server
	{
		listen 80;
   		access_log  /var/log/nginx/myproject.log;
   		error_log   /var/log/nginx/myproject.log;
		proxy_ignore_client_abort on;
		charset utf-8;		
		location ~^\/upload\/* {
        		root /var;
        		expires 30d;
    		}

		location /
         	{   
			proxy_cookie_path /myproject/ /;
                	proxy_set_header Host $host;
                	proxy_set_header X-Real-Ip $remote_addr;
                	proxy_set_header X-Forwarded-For $remote_addr;
                	proxy_pass http://localhost:8080/;
          	}   
	}

主要配置参数含义以下:

(留坑,明天有空来写~~~~~~~~~~~~~)



4 修改配置后reload:

sudo nginx -s reload



参考:

http://oilbeater.com/nginx/2014/12/29/nginx-conf-from-zero.html

http://www.cyberciti.biz/faq/nginx-restart-ubuntu-linux-command/

相关文章
相关标签/搜索