Код: Выделить всё
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <unistd.h>
#include <getopt.h>
#include <stdlib.h>
using namespace std;
int main (int argc, char *argv[]){
ofstream out("/tmp/config_nginx.ini"); //ofstream - это тип данных. Внутри скобок параметр, принимаемый конструктором объявленного объекта
out<<"user "<<argv[1]<<";\n"; // тут подставляю первую опцию (ключ)
out<<" \n";
out<<"# As a thumb rule: One per CPU. If you are serving a large amount\n";
out<<"# of static files, which requires blocking disk reads, you may want\n";
out<<"# to increase this from the number of cpu_cores available on your\n";
out<<"# system.\n";
out<<"#\n";
out<<"# The maximum number of connections for Nginx is calculated by:\n";
out<<"# max_clients = worker_processes * worker_connections\n";
out<<"worker_processes 1;\n";
out<<" \n";
out<<"# Maximum file descriptors that can be opened per process\n";
out<<"# This should be > worker_connections\n";
out<<"worker_rlimit_nofile 8192;\n";
out<<" \n";
out<<"events {\n";
out<<"# When you need > 8000 * cpu_cores connections, you start optimizing\n";
out<<"# your OS, and this is probably the point at where you hire people\n";
out<<"# who are smarter than you, this is *a lot* of requests.\n";
out<<"worker_connections 8000;\n";
out<<"}\n";
out<<" \n";
out<<"error_log /var/log/nginx/error.log;\n";
out<<" \n";
out<<"pid /var/run/nginx.pid;\n";
out<<" \n";
out<<"http {\n";
out<<"charset "<<argv[2]<<";\n"; // а здесь вторая опция(ключ)
out<<"\n";
out<<"\n";
out<<"\n";
out<<"ssl_session_cache shared:SSL:10m;\n";
out<<"ssl_session_timeout 5m;\n";
out<<"ssl_prefer_server_ciphers on;\n";
out<<"ssl_stapling on;\n";
out<<"resolver 8.8.8.8;\n";
out<<"\n";
out<<"\n";
out<<"\n";
out<<"\n";
out<<"\n";
out<<" \n";
out<<"# Set the mime-types via the mime.types external file\n";
out<<"include mime.types;\n";
out<<" \n";
out<<"# And the fallback mime-type\n";
out<<"default_type application/octet-stream;\n";
out<<" \n";
out<<"# Click tracking!\n";
out<<"access_log /var/log/nginx/access.log;\n";
out<<" \n";
out<<"# Hide nginx version\n";
out<<"server_tokens off;\n";
out<<" \n";
out<<"# ~2 seconds is often enough for HTML/CSS, but connections in\n";
out<<"# Nginx are cheap, so generally it's safe to increase it\n";
out<<"keepalive_timeout 20;\n";
out<<" \n";
out<<"# You usually want to serve static files with Nginx\n";
out<<"sendfile on;\n";
out<<" \n";
out<<"tcp_nopush on; # off may be better for Comet/long-poll stuff\n";
out<<"tcp_nodelay off; # on may be better for Comet/long-poll stuff\n";
out<<" \n";
out<<"server_name_in_redirect off;\n";
out<<"types_hash_max_size 2048;\n";
out<<" \n";
out<<"gzip on;\n";
out<<"gzip_http_version 1.0;\n";
out<<"gzip_comp_level 5;\n";
out<<"gzip_min_length 512;\n";
out<<"gzip_buffers 4 8k;\n";
out<<"gzip_proxied any;\n";
out<<"gzip_types\n";
out<<"# text/html is always compressed by HttpGzipModule\n";
out<<"text/css\n";
out<<"text/plain\n";
out<<"text/x-component\n";
out<<"application/javascript\n";
out<<"application/json\n";
out<<"application/xml\n";
out<<"application/xhtml+xml\n";
out<<"application/x-font-ttf\n";
out<<"application/x-font-opentype\n";
out<<"application/vnd.ms-fontobject\n";
out<<"image/svg+xml\n";
out<<"image/x-icon;\n";
out<<" \n";
out<<"# This should be turned on if you are going to have pre-compressed copies (.gz) of\n";
out<<"# static files available. If not it should be left off as it will cause extra I/O\n";
out<<"# for the check. It would be better to enable this in a location {} block for\n";
out<<"# a specific directory:\n";
out<<"# gzip_static on;\n";
out<<" \n";
out<<"gzip_disable msie6;\n";
out<<"gzip_vary on;\n";
out<<"# Upstream to abstract backend connection(s) for PHP\n";
out<<"upstream php {\n";
out<<"server unix:/tmp/php5-fpm.sock;\n";
out<<"}\n";
out<<" \n";
out<<"include /etc/nginx/conf.d/*.conf;\n";
out<<"include /etc/nginx/sites-enabled/*;\n";
out<<"}\n";
out<<"\n";
out.close(); //Закрываем файл
return 0;
}
Пример запуска:
./untitled15 dima cp1251
сгенереный конфиг выглядит следующим образом:
user dima;
# As a thumb rule: One per CPU. If you are serving a large amount
# of static files, which requires blocking disk reads, you may want
# to increase this from the number of cpu_cores available on your
# system.
#
# The maximum number of connections for Nginx is calculated by:
# max_clients = worker_processes * worker_connections
worker_processes 1;
# Maximum file descriptors that can be opened per process
# This should be > worker_connections
worker_rlimit_nofile 8192;
events {
# When you need > 8000 * cpu_cores connections, you start optimizing
# your OS, and this is probably the point at where you hire people
# who are smarter than you, this is *a lot* of requests.
worker_connections 8000;
}
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
http {
charset cp1251;
.......
Вопрос
Как сделать длинные опции(ключи) для работы с моим приложением по типу чтобы с приложением можно было работать
./untitled15 --user=dima --charset=cp1251